In Python, to perform async requests, we can use the asyncio
module. It allows us to write asynchronous, non-blocking code using coroutines and an event loop. We can also use libraries like python-requests
to make HTTP requests.
Here is an example of an async request using python-requests
:
main.py371 chars15 lines
In the above code, we define a coroutine fetch()
that fetches the content of a URL using the requests
library. We then define another coroutine main()
that creates an aiohttp
session and calls the fetch()
coroutine to make an async request.
Finally, we create an event loop using asyncio.get_event_loop()
and run the main()
coroutine using loop.run_until_complete()
. This will make the requests asynchronously and output the HTML content of the page.
gistlibby LogSnag