user1102018
user1102018

Reputation: 4451

gevent monkey patch breaks gevent.sleep(0)?

I'm asking this question after a full day of hacking and trying to figure out what's wrong here.
I want to send a request using the requests package, and do it in a non-blocking mode.
For that, I'm using the fine gevent package with its monkey patching abilities, which I have been using for a long time.

I have a main loop that receives a message and spawns a new greenlet that creates a POST request for every message. Following the spawn command, the main loop does a gevent.sleep(0) to allow the greenlets to do their work.

The code below is a simplified example (without the main loop):

from gevent import monkey; monkey.patch_all()
import gevent    # (version 1.0.2)
import requests  # version 2.7.0

def f():
    requests.post('http://localhost:8888/', data='*' * 80)

gevent.spawn(f)
gevent.sleep(0)

If you try to run if from the command line (or a file), the request will NOT be sent.
After playing around, I managed to get this code to send the request if I:

  1. Remove the monkey patching line (which is not good because then it will be blocking), or
  2. change the value of the sleep to 0.1 (which is not good because I really don't want the main loop to sleep).

Both options are a big NO for me.
Any chance someone knows why this strange behaviour and how do I fix it?

Upvotes: 1

Views: 576

Answers (2)

郑福真
郑福真

Reputation: 314

So, I think what you need is: gevent.joinall([g1, g2, ...]), but not gevent.sleep

Upvotes: 0

user1102018
user1102018

Reputation: 4451

Well, turns out it's the desired behaviour.
Here's a link to more details on this subject: https://github.com/gevent/gevent/issues/744

Upvotes: 1

Related Questions