Skip to content

Instantly share code, notes, and snippets.

@mrjoes
Created October 20, 2012 20:25
Show Gist options
  • Save mrjoes/3924669 to your computer and use it in GitHub Desktop.
Save mrjoes/3924669 to your computer and use it in GitHub Desktop.
1. See implementation here: https://gist.github.com/3891601
2. Python 2 code:
@gen.async
def long_op(a):
if a == 1:
yield gen.Task(io_loop.add_timeout, time() + 0.1)
gen.ret(a)
yield gen.Task(io_loop.add_timeout, time() + 0.1)
gen.ret(a * 10)
3. With vanilla tornado.gen it looks like:
@gen.engine
def long_op(a, callback):
if a == 1:
yield gen.Task(io_loop.add_timeout, time() + 0.1)
callback(a)
return
yield gen.Task(io_loop.add_timeout, time() + 0.1)
callback(a * 10)
4. Starting with Python 3.3, you can use return with argument - it will raise StopIteration
@gen.async
def long_op(a):
if a == 1:
yield gen.Task(io_loop.add_timeout, time() + 0.1)
return a
yield gen.Task(io_loop.add_timeout, time() + 0.1)
return a * 10
5. Obviously you can mix gen.engine and gen.async if you want to run callbacks explicitly or implicitly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment