encode / requests-async

async-await support for `requests`. ✨ 🍰 ✨

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blocking on network I/O, not co-operative

heemayl opened this issue · comments

Unless i'm missing something, this does not help with co-operative multitasking i.e. it does not let the event loop to run other coroutines while waiting on network I/O.

In [1397]: async def send_req(url): 
      ...:     print(f'Start: {url}: {time.time()}') 
      ...:     res = await requests_async.get(url) 
      ...:     print(f'End: {url}: {res.status_code}: {time.time()}') 
      ...:     return None 
      ...:                                                                                                                                                                                                  

In [1398]: async def check_requests_async(): 
      ...:     await send_req('https://example.com') 
      ...:     await send_req('https://python.org') 
      ...:     await send_req('https://pypi.org') 
      ...:     return None 
      ...:                                                                                                                                                                                                  

In [1399]: loop.run_until_complete(check_requests_async())                                                                                                                                                  
Start: https://example.com: 1553195192.1109543
End: https://example.com: 200: 1553195194.197162
Start: https://python.org: 1553195194.1972675
End: https://python.org: 200: 1553195198.2745004
Start: https://pypi.org: 1553195198.2746341
End: https://pypi.org: 200: 1553195200.1185997

Please correct me if I'm barking up the wrong tree here.

Yeah so - you’re making several async requests, but sequentially. (🌳 🐕 ) - I’ll add an example of how you can do simultaneous async requests. 😀👍

My bad; probably need some coffee ☕ . This works as expected:

In [1419]: async def check_requests_async(): 
      ...:     coros = [ 
      ...:         send_req('https://example.com'), 
      ...:         send_req('https://python.org'), 
      ...:         send_req('https://pypi.org') 
      ...:     ] 
      ...:     await asyncio.gather(*coros) 
      ...:     return None 
      ...:                                                                                                                                                                                                  

In [1420]: loop.run_until_complete(check_requests_async())                                                                                                                                                  
Start: https://example.com: 1553196752.9856997
Start: https://python.org: 1553196752.9877937
Start: https://pypi.org: 1553196752.9897845
End: https://pypi.org: 200: 1553196754.7989714
End: https://example.com: 200: 1553196756.6360314
End: https://python.org: 200: 1553196757.1897018

Thanks. 👍