Commit 9040b8ba authored by Denis Bilenko's avatar Denis Bilenko

greenlet.py: optimization for spawn and spawn_later; makes them 2-3 times faster

- avoid wrapper function if no kwargs
- use free function instead of closed one otherwise
parent b0f3b554
......@@ -34,17 +34,33 @@ _threadlocal = threadlocal()
_threadlocal.Hub = None
def _switch_helper(function, args, kwargs):
return function(*args, **kwargs)
def spawn(function, *args, **kwargs):
g = Greenlet(lambda : function(*args, **kwargs))
if kwargs:
g = Greenlet(_switch_helper)
g.parent = get_hub().greenlet
core.active_event(g.switch, function, args, kwargs)
return g
else:
g = Greenlet(function)
g.parent = get_hub().greenlet
core.active_event(g.switch)
core.active_event(g.switch, *args)
return g
def spawn_later(seconds, function, *args, **kwargs):
g = Greenlet(lambda : function(*args, **kwargs))
if kwargs:
g = Greenlet(_switch_helper)
g.parent = get_hub().greenlet
core.timer(0, g.switch, function, args, kwargs)
return g
else:
g = Greenlet(function)
g.parent = get_hub().greenlet
core.timer(seconds, g.switch)
core.timer(0, g.switch, *args)
return g
......
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