Possible Duplicate:
Asynchronous Requests with Python requests
Is the python module Requests non-blocking? I don’t see anything in the docs about blocking or non-blocking.
If it is blocking, which module would you suggest?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Like
urllib2,requestsis blocking.But I wouldn’t suggest using another library, either.
The simplest answer is to run each request in a separate thread. Unless you have hundreds of them, this should be fine. (How many hundreds is too many depends on your platform. On Windows, the limit is probably how much memory you have for thread stacks; on most other platforms the cutoff comes earlier.)
If you do have hundreds, you can put them in a threadpool. The
ThreadPoolExecutorExample in theconcurrent.futurespage is almost exactly what you need; just change theurllibcalls torequestscalls. (If you’re on 2.x, usefutures, the backport of the same packages on PyPI.) The downside is that you don’t actually kick off all 1000 requests at once, just the first, say, 8.If you have hundreds, and they all need to be in parallel, this sounds like a job for
gevent. Have it monkeypatch everything, then write the exact same code you’d write with threads, but spawninggreenlets instead ofThreads.grequests, which evolved out of the old async support directly inrequests, effectively does thegevent+requestswrapping for you. And for the simplest cases, it’s great. But for anything non-trivial, I find it easier to read explicitgeventcode. Your mileage may vary.Of course if you need to do something really fancy, you probably want to go to
twisted,tornado, ortulip(or wait a few months fortulipto be part of the stdlib).