Commit 5dba8d39 authored by Yury Selivanov's avatar Yury Selivanov

Actually call spawn callbacks in Greenlet.start() and start_later()

(not in spawn() and spawn_later())
parent 9e8833e6
......@@ -508,6 +508,7 @@ class Greenlet(greenlet):
def start(self):
"""Schedule the greenlet to run in this loop iteration"""
if self._start_event is None:
_call_spawn_callbacks(self)
self._start_event = self.parent.loop.run_callback(self.switch)
def start_later(self, seconds):
......@@ -518,6 +519,7 @@ class Greenlet(greenlet):
*seconds* later
"""
if self._start_event is None:
_call_spawn_callbacks(self)
self._start_event = self.parent.loop.timer(seconds)
self._start_event.start(self.switch)
......@@ -572,7 +574,6 @@ class Greenlet(greenlet):
instead of spawning a greenlet that will raise an uncaught TypeError.
"""
g = cls(*args, **kwargs)
_call_spawn_callbacks(g)
g.start()
return g
......@@ -596,7 +597,6 @@ class Greenlet(greenlet):
if cls is Greenlet and not args and 'run' not in kwargs:
raise TypeError("")
g = cls(*args, **kwargs)
_call_spawn_callbacks(g)
g.start_later(seconds)
return g
......
......@@ -700,7 +700,11 @@ class TestBasic(greentest.TestCase):
gevent.sleep(0.01)
def test_add_spawn_callback(self):
called = 0
def cb(gr):
nonlocal called
called += 1
gr._called_test = True
gevent.Greenlet.add_spawn_callback(cb)
......@@ -708,11 +712,24 @@ class TestBasic(greentest.TestCase):
g = gevent.spawn(lambda: None)
self.assertTrue(hasattr(g, '_called_test'))
g.join()
self.assertEqual(called, 1)
g = gevent.spawn_later(1e-5, lambda: None)
self.assertTrue(hasattr(g, '_called_test'))
g.join()
self.assertEqual(called, 2)
g = gevent.Greenlet(lambda: None)
g.start()
self.assertTrue(hasattr(g, '_called_test'))
g.join()
self.assertEqual(called, 3)
gevent.Greenlet.remove_spawn_callback(cb)
g = gevent.spawn(lambda: None)
self.assertFalse(hasattr(g, '_called_test'))
g.join()
self.assertEqual(called, 3)
finally:
gevent.Greenlet.remove_spawn_callback(cb)
......
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