I'd like to issue an asynchronous HTTP POST request using gevent
-- I don't care about the response, I simply want to execute the request as soon as possible. However, whenever I attempt to do so using gevent.spawn
, the request never executes. I know this because calling the .ready()
or .successful()
methods on the Greenlet
that is returned from gevent.spawn
always returns False
.
我想使用gevent发出一个异步HTTP POST请求——我不关心响应,我只想尽快执行请求。但是,每当我尝试使用gevent时。衍生,请求永远不会执行。我之所以知道这一点,是因为在gevent返回的Greenlet上调用.ready()或.success()方法。产卵总是返回False。
However, the Greenlet has started, because if I call glet = gevent.spawn(...)
, then glet.start()
, I get an error saying AssertionError: Greenlet already started
.
然而,Greenlet已经启动,因为如果我调用glet = gevent.spawn(…),然后glet.start(),就会得到一个输入AssertionError: Greenlet已经启动的错误。
The only time I get a glet.ready() == True
is when I call glet.join()
, but this is a blocking operation. How can I have the Greenlet execute without waiting for it to complete?
我得到一个glet.ready() = True的唯一时间是当我调用glet.join()时,但是这是一个阻塞操作。我怎么能让绿线执行而不等待它完成?
1 个解决方案
#1
7
Since greenlets are cooperative, your new greenlet won't run until you yield to it. After calling spawn, call gevent.sleep(0)
to yield and your greenlet should run.
因为greenlets是合作的,所以你的新greenlet不会跑到你屈服为止。在调用spawn之后,调用gevent.sleep(0)来yield,您的greenlet应该运行。
It will continue to run until it does something that causes it to yield (like kicking off that http req). Then your other code can resume again.
它将继续运行,直到它做一些导致它屈服的事情(比如启动http req)。然后你的其他代码可以重新开始。
EDIT:
编辑:
To address your question about grequests
, the grequests.send()
doc says:
要回答你的问题,grequest。send() doc说:
send(r, pool=None, stream=False)
Sends the request object using the specified pool. If a pool isn't
specified this method blocks. Pools are useful because you can specify size
and can hence limit concurrency
Since you have not specified a pool, the request blocks for your greenlet to finish. In other words, once it returns the greenlet has already completed. To get the response, see glt.get()
of the returned greenlet.
由于尚未指定池,因此请求将阻塞您的greenlet以完成。换句话说,一旦它返回,绿点就已经完成了。要获得响应,请参阅glt.get()返回的greenlet。
#1
7
Since greenlets are cooperative, your new greenlet won't run until you yield to it. After calling spawn, call gevent.sleep(0)
to yield and your greenlet should run.
因为greenlets是合作的,所以你的新greenlet不会跑到你屈服为止。在调用spawn之后,调用gevent.sleep(0)来yield,您的greenlet应该运行。
It will continue to run until it does something that causes it to yield (like kicking off that http req). Then your other code can resume again.
它将继续运行,直到它做一些导致它屈服的事情(比如启动http req)。然后你的其他代码可以重新开始。
EDIT:
编辑:
To address your question about grequests
, the grequests.send()
doc says:
要回答你的问题,grequest。send() doc说:
send(r, pool=None, stream=False)
Sends the request object using the specified pool. If a pool isn't
specified this method blocks. Pools are useful because you can specify size
and can hence limit concurrency
Since you have not specified a pool, the request blocks for your greenlet to finish. In other words, once it returns the greenlet has already completed. To get the response, see glt.get()
of the returned greenlet.
由于尚未指定池,因此请求将阻塞您的greenlet以完成。换句话说,一旦它返回,绿点就已经完成了。要获得响应,请参阅glt.get()返回的greenlet。