python - How could I use requests in asyncio? - Stack Overflow

PHOTO EMBED

Tue Dec 20 2022 07:17:16 GMT+0000 (Coordinated Universal Time)

Saved by @justquick #python

import asyncio
import concurrent.futures
import requests

async def main():

    with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:

        loop = asyncio.get_event_loop()
        futures = [
            loop.run_in_executor(
                executor, 
                requests.get, 
                'http://example.org/'
            )
            for i in range(20)
        ]
        for response in await asyncio.gather(*futures):
            pass


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
content_copyCOPY

https://stackoverflow.com/questions/22190403/how-could-i-use-requests-in-asyncio