"ile zaman uyumsuz" aiohttp için Başlangıç dokümanlar aşağıdaki istemci örnek vermek Alma:Python 3.4'te
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)
Ve onlar Python 3.4 kullanıcıları için aşağıdaki notu vermek: Eğer kullanıyorsanız
Python 3.4, lütfen @coroutine dekolayıcısından async def ile beklemeden bekleyiniz. Ben bu talimatları izlerseniz
alıyorum:
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)
Ancak bu yayınlanmaz, async with
Python 3.4 desteklenmediği için:
$ python3 client.py
File "client.py", line 7
async with session.get(url) as response:
^
SyntaxError: invalid syntax
Ben async with
çevirebilir nasıl Python 3.4 ile çalışmak için açıklama?
Bu örneklere işaret ettiğiniz için teşekkür ederiz. Ben bunları bulamadım. – Imran
Bir istisna durumunda, genellikle yanıtı * kapatmak istediğinizi unutmayın. –
@MartijnPieters teşekkürler, haklısınız. Kodu _RequestContextManager .__ aexit__' mantığıyla eşleştirdim. –