dal*_*ler 19 python python-asyncio python-3.5
我正在使用asyncioPython 3.5中的使用流程,但我还没有看到我应该做什么await以及我不应该做的事情或者可以忽略不计的事情的描述.我是否必须在"这是一个IO操作,因此应该await编辑" 方面使用我的最佳判断?
Mik*_*mov 56
默认情况下,所有代码都是同步的.您可以使用异步定义函数async def并"调用"此函数await.更正确的问题是"我应该何时编写异步代码而不是同步?".答案是"当你能从中受益"时.在大多数情况下,正如您所指出的,当您使用I/O操作时,您将获益:
# Synchronous way:
download(url1) # takes 5 sec.
download(url2) # takes 5 sec.
# Total time: 10 sec.
# Asynchronous way:
await asyncio.gather(
async_download(url1), # takes 5 sec.
async_download(url2) # takes 5 sec.
)
# Total time: only 5 sec. (+ little overhead for using asyncio)
Run Code Online (Sandbox Code Playgroud)
当然,如果你创建了使用异步代码的函数,那么这个函数也应该是异步的(应该定义为async def).但任何异步函数都可以自由使用同步代码.没有某些原因将同步代码转换为异步是没有意义的:
# extract_links(url) should be async because it uses async func async_download() inside
async def extract_links(url):
# async_download() was created async to get benefit of I/O
html = await async_download(url)
# parse() doesn't work with I/O, there's no sense to make it async
links = parse(html)
return links
Run Code Online (Sandbox Code Playgroud)
一个非常重要的事情是,任何长时间的同步操作(例如,> 50 ms,很难确切地说)都会冻结那段时间的所有异步操作:
async def extract_links(url):
data = await download(url)
links = parse(data)
# if search_in_very_big_file() takes much time to process,
# all your running async funcs (somewhere else in code) will be frozen
# you need to avoid this situation
links_found = search_in_very_big_file(links)
Run Code Online (Sandbox Code Playgroud)
您可以避免它在单独的进程中调用长时间运行的同步函数(并等待结果):
executor = ProcessPoolExecutor(2)
async def extract_links(url):
data = await download(url)
links = parse(data)
# Now your main process can handle another async functions while separate process running
links_found = await loop.run_in_executor(executor, search_in_very_big_file, links)
Run Code Online (Sandbox Code Playgroud)
还有一个例子:当你需要requests在asyncio中使用时.requests.get只是同步长时间运行的功能,你不应该在异步代码内调用(再次,以避免冻结).但是由于I/O,它运行时间很长,而不是因为长时间的计算.在这种情况下,您可以使用ThreadPoolExecutor而不是ProcessPoolExecutor避免一些多处理开销:
executor = ThreadPoolExecutor(2)
async def download(url):
response = await loop.run_in_executor(executor, requests.get, url)
return response.text
Run Code Online (Sandbox Code Playgroud)