"के साथ async" हो रही aiohttp के लिए शुरू किया डॉक्स निम्न क्लाइंट उदाहरण दे:अजगर 3.4 में
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)
और वे अजगर 3.4 उपयोगकर्ताओं के लिए निम्नलिखित नोट दे:
आप उपयोग कर रहे हैं पायथन 3.4, कृपया @एसिंक डीसी @ कोराउटिन सजावट के साथ उपज के साथ प्रतीक्षा करें।
अगर मैं इन निर्देशों का पालन मैं:
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)
बहरहाल, यह नहीं चलेगा, क्योंकि async with
अजगर 3.4 में समर्थित नहीं है:
$ python3 client.py
File "client.py", line 7
async with session.get(url) as response:
^
SyntaxError: invalid syntax
मैं async with
अनुवाद कर सकते हैं कैसे पायथन 3.4 के साथ काम करने के लिए बयान?
मुझे उन उदाहरणों को इंगित करने के लिए धन्यवाद। मुझे वह नहीं मिला था। – Imran
ध्यान दें कि एक अपवाद के मामले में आप आमतौर पर प्रतिक्रिया * बंद करना चाहते हैं। –
@MartijnPieters धन्यवाद, आप सही हैं। मैंने '_RequestContextManager .__ aexit__' तर्क से मेल खाने के लिए कोड तय किया। –