默认情况下,所有代码都是同步的。你可以使用使其异步定义函数,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() insideasync 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 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 pre) 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)
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)