Python 3.5 async/await with real code example

后端 未结 2 1662
情话喂你
情话喂你 2021-01-30 14:00

I\'ve read tons of articles and tutorial about Python\'s 3.5 async/await thing. I have to say I\'m pretty confused, because some use get_event_loop() and run_until_complete(), s

2条回答
  •  走了就别回头了
    2021-01-30 14:41

    You can take a look at the following simple working example from here. By the way it returns a string worth reading :-)

    import aiohttp
    import asyncio
    
    async def fetch(client):
      async with client.get('https://docs.aiohttp.org/en/stable/client_reference.html') as resp:
        assert resp.status == 200
        return await resp.text()
    
    async def main():
      async with aiohttp.ClientSession() as client:
        html = await fetch(client)
        print(html)
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    

提交回复
热议问题