YieldPoints: Simple extensions to Tornado’s gen module.

_images/yield.png

Simple extensions to Tornado’s gen module.

Examples

Use WaitAny to begin two tasks and handle their results in the order completed:

>>> @gen.coroutine
... def f():
...     callback0 = yield gen.Callback(0)
...     callback1 = yield gen.Callback(1)
...
...     # Fire callback1 soon, callback0 later
...     IOLoop.current().add_timeout(
...         timedelta(seconds=0.1), partial(callback1, 'foo'))
...
...     IOLoop.current().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.current().run_sync(f)
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.coroutine
... def f():
...     yield gen.Callback('key') # never called
...     yield yieldpoints.Cancel('key')
...
>>> IOLoop.current().run_sync(f)

Wait with a timeout. WithTimeout can take a YieldPoint or a key name as its second argument:

>>> @gen.coroutine
... 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.current().run_sync(f)
Timeout!

Bug Reports and Feature Requests

Also on GitHub: https://github.com/ajdavis/yieldpoints/issues

Indices and tables