"Asíncrono con" en Python 3.4
Los documentos de introducción para aiohttp ofrecen el siguiente ejemplo de cliente:
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)
Y dan la siguiente nota para los usuarios de Python 3.4:
Si está utilizando Python 3.4, reemplace waitit con rendimiento de y async def con un decorador @coroutine.
Si sigo estas instrucciones me sale:
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)
Sin embargo, esto no se ejecutará porqueasync with
no es compatible con Python 3.4:
$ python3 client.py
File "client.py", line 7
async with session.get(url) as response:
^
SyntaxError: invalid syntax
¿Cómo puedo traducir elasync with
declaración para trabajar con Python 3.4?