Commit 5abb82fd authored by Denis Bilenko's avatar Denis Bilenko

add idle() function; make sleep(0) use idle(priority=MAXPRI) to avoid sleeping on windows

also make sleep(negative) equivalent to sleep(0); raising an error really has no use here;
parent d879e664
......@@ -24,6 +24,7 @@ __all__ = ['get_hub',
'with_timeout',
'getcurrent',
'sleep',
'idle',
'kill',
'signal',
'fork',
......@@ -45,7 +46,7 @@ spawn_link = Greenlet.spawn_link
spawn_link_value = Greenlet.spawn_link_value
spawn_link_exception = Greenlet.spawn_link_exception
from gevent.timeout import Timeout, with_timeout
from gevent.hub import getcurrent, GreenletExit, spawn_raw, sleep, kill, signal
from gevent.hub import getcurrent, GreenletExit, spawn_raw, sleep, idle, kill, signal
try:
from gevent.hub import fork
except ImportError:
......
......@@ -71,13 +71,28 @@ def sleep(seconds=0):
"""Put the current greenlet to sleep for at least *seconds*.
*seconds* may be specified as an integer, or a float if fractional seconds
are desired. Calling sleep with *seconds* of 0 is the canonical way of
expressing a cooperative yield.
are desired.
If *seconds* is equal to or less than zero, yield control the other coroutines
without actually putting the process to sleep. The :class:`core.idle` watcher
with the highest priority is used to achieve that.
"""
if not seconds >= 0:
raise IOError(22, 'Invalid argument')
hub = get_hub()
hub.wait(hub.loop.timer(seconds))
loop = hub.loop
if seconds <= 0:
watcher = loop.idle()
watcher.priority = loop.MAXPRI
else:
watcher = loop.timer(seconds)
hub.wait(watcher)
def idle(priority=0):
hub = get_hub()
watcher = hub.loop.idle()
if priority:
watcher.priority = priority
hub.wait(watcher)
def kill(greenlet, exception=GreenletExit):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment