In what way is grequests asynchronous?

后端 未结 3 439
余生分开走
余生分开走 2020-12-23 02:41

I\'ve been using the python requests library for some time, and recently had a need to make a request asynchronously, meaning I would like to send off the HTTP request, have

3条回答
  •  时光说笑
    2020-12-23 03:06

    .map() is meant to run retrieval of several URLs in parallel, and will indeed wait for these tasks to complete (gevent.joinall(jobs)) is called).

    Use .send() instead to spawn jobs, using a Pool instance:

    req = grequests.get('http://www.codehenge.net/blog', hooks=dict(response=print_res))
    job = grequests.send(req, grequests.Pool(1))
    
    for i in range(10):
        print i
    

    Without the pool the .send() call will block still, but only for the gevent.spawn() call it executes.

提交回复
热议问题