何时使用以及何时不使用Python 3.5`await`?

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)

  • @AnnaVopureta `loop = asyncio.get_event_loop()` (文档在[此处](https://docs.python.org/3/library/asyncio-eventloop.html#event-loop)) (7认同)
  • @Stallman在异步编程中通过“冻结”来表示,我的意思是任何需要花费大量时间(> 50毫秒)执行的非异步函数。“ requests.get(url)”,“ time.sleep(1)”是此类功能的示例。从主线程执行此类函数时,事件循环无法继续在其他任何地方执行协程。因此,在示例中,等待答案的第一个版本的“ extract_links”将阻止在其他代码部分执行协程。为了避免它,第二版的`extract_links`在后台线程中运行冻结功能。 (3认同)
  • 或者您可以使用[aiohttp](https://pypi.org/project/aiohttp/),这是一个设计用于与asyncio一起使用的请求包。 (3认同)
  • @buhtz这只是我用作示例的任意时间。无法计算具体数量,但主要思想是,如果在此时间或更长时间内阻止事件循环,可能会对其他协程成功​​产生潜在危害。想象一下当您以10秒的超时时间启动异步请求并立即阻止事件循环11秒的情况:请求将超时,而本来可以成功。为避免这种情况,您应确保将控制权返回事件循环的时间不少于一些小的时间(例如50毫秒)。 (2认同)