Simple extensions to Tornado’s gen module.
Use WaitAny to begin two tasks and handle their results in the order completed:
>>> @gen.engine
... def f():
... callback0 = yield gen.Callback(0)
... callback1 = yield gen.Callback(1)
...
... # Fire callback1 soon, callback0 later
... IOLoop.instance().add_timeout(
... timedelta(seconds=0.1), partial(callback1, 'foo'))
...
... IOLoop.instance().add_timeout(
... timedelta(seconds=0.2), partial(callback0, 'bar'))
...
... keys = set([0, 1])
... while keys:
... key, result = yield yieldpoints.WaitAny(keys)
... print 'key:', key, ', result:', result
... keys.remove(key)
... IOLoop.instance().stop()
...
>>> f()
>>> IOLoop.instance().start()
key: 1 , result: foo
key: 0 , result: bar
If you begin a task but don’t wait for it, use Cancel or to CancelAll avoid a LeakedCallbackError:
>>> @gen.engine
... def f():
... yield gen.Callback('key') # never called
... yield yieldpoints.Cancel('key')
... IOLoop.instance().stop()
...
>>> f()
>>> IOLoop.instance().start()
Wait with a timeout. WithTimeout can take a YieldPoint or a key name as its second argument:
>>> @gen.engine
... def f():
... callback = yield gen.Callback('key') # never called
... try:
... key, result = yield yieldpoints.WithTimeout(
... timedelta(seconds=0.1), 'key')
... except yieldpoints.TimeoutException:
... print 'Timeout!'
... IOLoop.instance().stop()
...
>>> f()
>>> IOLoop.instance().start()
Timeout!
Is on GitHub: https://github.com/ajdavis/yieldpoints
Also on GitHub: https://github.com/ajdavis/yieldpoints/issues