Menu Close

How to make asynchronous requests with Python requests module?

Sometimes, we want to make asynchronous requests with Python.

In this article, we’ll look at how to make asynchronous requests with Python.

How to make asynchronous requests with Python?

To make asynchronous requests with Python, we can use the grequests module.

For instance, we write:

import grequests

urls = [
    'http://www.heroku.com',
    'http://tablib.org',
    'http://httpbin.org',
    'http://python-requests.org',
    'http://kennethreitz.com'
]

rs = (grequests.get(u) for u in urls)
r = grequests.map(rs)
print(r)

We call grequests.get with the entries in the urls list.

And then we assign the responses to rs.

Then we call grequests.map with rs to make all the requests at once and assign the list of responses to r.

Therefore, r is [<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>].

Conclusion

To make asynchronous requests with Python, we can use the grequests module.

Posted in Python, Python Answers