何时使用以及何时不使用Python 3.5`await`?
作者:互联网
我在Python 3.5中得到了使用asyncio的流程,但是我还没有看到我应该等待什么,以及我不应该做的事情或者可以忽略不计的事情的描述.我是否必须在“这是IO操作,因此应该等待”方面使用我的最佳判断?
解决方法:
默认情况下,所有代码都是同步的.您可以使用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)
当然,如果你创建了使用异步代码的函数,那么这个函数也应该是异步的(应该定义为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
一个非常重要的事情是任何长时间的同步操作(例如,> 50毫秒,很难确切地说)将冻结那段时间的所有异步操作:
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)
您可以避免它在单独的进程中调用长时间运行的同步函数(并等待结果):
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)
还有一个例子:当你需要在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
标签:python-3-5,python,python-asyncio 来源: https://codeday.me/bug/20190911/1804379.html