Commit cb9e7f63 authored by Denis Bilenko's avatar Denis Bilenko

add gevent.spawn_raw; remove rawgreenlet.spawn and rawgreenlet.spawn_later

parent 75da912c
......@@ -8,6 +8,7 @@ __all__ = ['Greenlet',
'spawn_link',
'spawn_link_value',
'spawn_link_exception',
'spawn_raw',
'joinall',
'killall',
'Timeout',
......@@ -29,7 +30,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, sleep, kill, signal, shutdown
from gevent.hub import getcurrent, GreenletExit, spawn_raw, sleep, kill, signal, shutdown
try:
from gevent.hub import fork
except ImportError:
......
......@@ -26,8 +26,7 @@ import time
import traceback
from gevent.core import active_event
from gevent.hub import get_hub, getcurrent, sleep
from gevent.rawgreenlet import spawn
from gevent.hub import get_hub, getcurrent, sleep, spawn_raw
class Cancelled(RuntimeError):
......@@ -54,7 +53,7 @@ class event(object):
>>> def baz(b):
... evt.send(b + 1)
...
>>> _ = spawn(baz, 3)
>>> _ = spawn_raw(baz, 3)
>>> evt.wait()
4
"""
......@@ -138,7 +137,7 @@ class event(object):
>>> def wait_on():
... retval = evt.wait()
... print "waited for", retval
>>> _ = spawn(wait_on)
>>> _ = spawn_raw(wait_on)
>>> evt.send('result')
>>> sleep(0)
waited for result
......@@ -168,7 +167,7 @@ class event(object):
... print 'about to wait'
... result = evt.wait()
... print 'waited for', result
>>> _ = spawn(waiter)
>>> _ = spawn_raw(waiter)
>>> sleep(0)
about to wait
>>> evt.send('a')
......@@ -350,7 +349,7 @@ def execute(func, *args, **kw):
evt = event()
def _really_execute():
evt.send(func(*args, **kw))
spawn(_really_execute)
spawn_raw(_really_execute)
return evt
......
......@@ -6,6 +6,7 @@ from gevent import core
__all__ = ['getcurrent',
'GreenletExit',
'spawn_raw',
'sleep',
'kill',
'signal',
......@@ -33,6 +34,22 @@ except AttributeError:
__all__.remove('fork')
def _switch_helper(function, args, kwargs):
# work around the fact that greenlet.switch does not support keyword args
return function(*args, **kwargs)
def spawn_raw(function, *args, **kwargs):
if kwargs:
g = greenlet(_switch_helper, get_hub())
core.active_event(g.switch, function, args, kwargs)
return g
else:
g = greenlet(function, get_hub())
core.active_event(g.switch, *args)
return g
def sleep(seconds=0):
"""Yield control to another eligible coroutine until at least *seconds* have
elapsed.
......
......@@ -575,7 +575,7 @@ class Proc(Source):
assert self.greenlet is None, "'run' can only be called once per instance"
if self.name is None:
self.name = str(function)
self.greenlet = rawgreenlet.spawn(self._run, function, args, kwargs)
self.greenlet = hub.spawn_raw(self._run, function, args, kwargs)
def _run(self, function, args, kwargs):
"""Internal top level function.
......@@ -818,7 +818,7 @@ class Pool(object):
def execute_async(self, func, *args, **kwargs):
if self.sem.locked():
return rawgreenlet.spawn(self.execute, func, *args, **kwargs)
return hub.spawn_raw(self.execute, func, *args, **kwargs)
else:
return self.execute(func, *args, **kwargs)
......
"""A few utilities for raw greenlets"""
"""A few utilities for raw greenlets.
# XXX these functions do not support 'timeout' parameter as gevent.greenlet API
# XXX this module does seem to be useful and may be removed in the future
"""
import traceback
from gevent import core
from gevent.hub import greenlet, get_hub, GreenletExit, Waiter, sleep
from gevent.hub import GreenletExit, Waiter, sleep
__all__ = ['spawn',
'spawn_later',
'kill',
__all__ = ['kill',
'killall',
'join',
'joinall']
def _switch_helper(function, args, kwargs):
# work around the fact that greenlet.switch does not support keyword args
return function(*args, **kwargs)
def spawn(function, *args, **kwargs):
if kwargs:
g = greenlet(_switch_helper, get_hub())
core.active_event(g.switch, function, args, kwargs)
return g
else:
g = greenlet(function, get_hub())
core.active_event(g.switch, *args)
return g
def spawn_later(seconds, function, *args, **kwargs):
if kwargs:
g = greenlet(_switch_helper, get_hub())
core.timer(seconds, g.switch, function, args, kwargs)
return g
else:
g = greenlet(function, get_hub())
core.timer(seconds, g.switch, *args)
return g
def _kill(greenlet, exception, waiter):
try:
greenlet.throw(exception)
......
......@@ -35,8 +35,7 @@ import sys
import errno
import time
from gevent.hub import getcurrent, get_hub
from gevent.rawgreenlet import spawn
from gevent.hub import getcurrent, get_hub, spawn_raw
from gevent import core
BUFFER_SIZE = 4096
......@@ -520,7 +519,7 @@ def tcp_server(listensocket, server, *args, **kw):
try:
while True:
client_socket = listensocket.accept()
spawn(server, client_socket, *args, **kw)
spawn_raw(server, client_socket, *args, **kw)
except error, e:
# Broken pipe means it was shutdown
if e[0] != 32:
......
"""implements standard module 'thread' with greenlets"""
__thread = __import__('thread')
from gevent.hub import getcurrent, GreenletExit
from gevent.rawgreenlet import spawn
from gevent.hub import getcurrent, GreenletExit, spawn_raw
from gevent.coros import Semaphore as LockType
def get_ident(gr=None):
......@@ -11,7 +10,7 @@ def get_ident(gr=None):
return id(gr)
def start_new_thread(function, args=(), kwargs={}):
g = spawn(function, *args, **kwargs)
g = spawn_raw(function, *args, **kwargs)
return get_ident(g)
def allocate_lock():
......
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