问题描述
我使用python请求库已有一段时间了,最近需要异步发出请求,这意味着我想发送HTTP请求,让我的主线程继续执行,并进行回调请求返回时调用.
自然地,我被带到grequests库( https://github.com/kennethreitz/grequests ),但是我对此行为感到困惑.例如:
import grequests
def print_res(res):
from pprint import pprint
pprint (vars(res))
req = grequests.get('http://www.codehenge.net/blog', hooks=dict(response=print_res))
res = grequests.map([req])
for i in range(10):
print i
上面的代码将产生以下输出:
<...large HTTP response output...>
0
1
2
3
4
5
6
7
8
9
grequests.map()调用显然会阻塞,直到HTTP响应可用为止.似乎我在这里误解了异步"行为,而grequests库仅用于同时执行多个HTTP请求并将所有响应发送到单个回调.这是正确的吗?
.map()
旨在并行运行多个URL的检索,并且确实会等待这些任务完成(调用gevent.joinall(jobs)
). /p>
使用.send()
代替,使用 Pool
实例:
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
在没有池的情况下,.send()
调用将仍然阻塞,但仅针对执行的gevent.spawn()
调用.
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 my main thread continue to execute, and have a callback called when the request returns.
Naturally, I was lead to the grequests library (https://github.com/kennethreitz/grequests), but i'm confused about the behavior. For example:
import grequests
def print_res(res):
from pprint import pprint
pprint (vars(res))
req = grequests.get('http://www.codehenge.net/blog', hooks=dict(response=print_res))
res = grequests.map([req])
for i in range(10):
print i
The above code will produce the following output:
<...large HTTP response output...>
0
1
2
3
4
5
6
7
8
9
The grequests.map() call obviously blocks until the HTTP response is available. It seems likely I misunderstood the 'asynchronous' behavior here, and the grequests library is just for performing multiple HTTP requests concurrently and sending all responses to a single callback. Is this accurate?
.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.
这篇关于grequests以哪种方式异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!