I would essentially like gevent.sleep
to behave like time.sleep
in that it returns execution after exactly (not at least) some number of seconds, but allow other Greenlets to run in the meantime.
An example:
import gevent
def a():
while True:
print "A"
gevent.sleep(0.2)
def b():
gevent.spawn(a)
while True:
print "B"
gevent.sleep(1)
b()
# often get more than 10 or 20 'A's for each 'B', would expect close to 5
Is there a better way to get the desired behaviour?
Your code is perfectly fine. I ran it on my linux machine and had this output:
B A A A A A B A A A A A B A A A A A B A A A A A B A A A A A B A A A A A
So it works very reliably on my end, running gevent 1.0.1 and python2.7. If it does not work on your end, you might want to try and update python or gevent.