Python 3.4中的“async with”

aiohttp的入门文档给出了以下客户端示例:

import asyncio
import aiohttp

async def fetch_page(session, url):
    with aiohttp.Timeout(10):
        async with session.get(url) as response:
            assert response.status == 200
            return await response.read()

loop = asyncio.get_event_loop()
with aiohttp.ClientSession(loop=loop) as session:
    content = loop.run_until_complete(
        fetch_page(session, 'http://python.org'))
    print(content)

他们为Python 3.4用户提供了以下注释:

如果您使用Python 3.4,请使用@coroutine修饰器替换await中的yield和async def。

如果我遵循这些指示,我会得到:

import aiohttp
import asyncio

@asyncio.coroutine
def fetch(session, url):
    with aiohttp.Timeout(10):
        async with session.get(url) as response:
            return (yield from response.text())

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    with aiohttp.ClientSession(loop=loop) as session:
        html = loop.run_until_complete(
            fetch(session, 'http://python.org'))
        print(html)

但是,这不会运行,因为Python 3.4中不支持async with

$ python3 client.py 
  File "client.py", line 7
    async with session.get(url) as response:
             ^
SyntaxError: invalid syntax

我如何将async with声明与Python 3.4一起使用?


只是不要使用session.get()的结果作为上下文管理器; 直接使用它作为协程。 session.get()产生的请求上下文管理器通常在退出时释放请求,但使用response.text()也是如此,所以你可以在这里忽略它:

@asyncio.coroutine
def fetch(session, url):
    with aiohttp.Timeout(10):
        response = yield from session.get(url)
        return (yield from response.text())

这里返回的请求封装器没有所需的异步方法( __aenter____aexit__ ),它们在不使用Python 3.5时完全省略(请参阅相关源代码)。

如果你在session.get()调用和访问response.text()之间有更多的语句可用,你可能想用try:..finally:无论如何释放连接; 如果发生异常,Python 3.5版本上下文管理器也会关闭响应。 因为在这里需要yield from response.release()yield from response.release() ,所以在Python 3.4之前不能在上下文管理器中封装它:

import sys

@asyncio.coroutine
def fetch(session, url):
    with aiohttp.Timeout(10):
        response = yield from session.get(url)
        try:
            # other statements
            return (yield from response.text())
        finally:
            if sys.exc_info()[0] is not None:
                # on exceptions, close the connection altogether
                response.close()
            else:
                yield from response.release()

aiohttp的例子使用3.4语法实现。 基于json客户端的例子,你的功能是:

@asyncio.coroutine
def fetch(session, url):
    with aiohttp.Timeout(10):
        resp = yield from session.get(url)
        try:
            return (yield from resp.text())
        finally:
            yield from resp.release()

UPD:

请注意,Martijn的解决方案适用于简单情况,但在特定情况下可能会导致不需要的行为:

@asyncio.coroutine
def fetch(session, url):
    with aiohttp.Timeout(5):
        response = yield from session.get(url)

        # Any actions that may lead to error:
        1/0

        return (yield from response.text())

# exception + warning "Unclosed response"

除了例外,您还会收到警告“未公开回复”。 这可能会导致复杂应用程序中的连接泄漏。 如果您手动调用resp.release() / resp.close()您将避免此问题:

@asyncio.coroutine
def fetch(session, url):
    with aiohttp.Timeout(5):
        resp = yield from session.get(url)
        try:

            # Any actions that may lead to error:
            1/0

            return (yield from resp.text())
        except Exception as e:
            # .close() on exception.
            resp.close()
            raise e
        finally:
            # .release() otherwise to return connection into free connection pool.
            # It's ok to release closed response:
            # https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/client_reqrep.py#L664
            yield from resp.release()

# exception only

我认为最好遵循官方示例(和__aexit__实现),并明确地调用resp.release() / resp.close()

链接地址: http://www.djcxy.com/p/53243.html

上一篇: "async with" in Python 3.4

下一篇: based coroutine versus native coroutine