Skip to content

Instantly share code, notes, and snippets.

@akheron
Created April 7, 2011 13:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akheron/907770 to your computer and use it in GitHub Desktop.
Save akheron/907770 to your computer and use it in GitHub Desktop.
Asynchronous map for Tornado's ioloop
from tornado.ioloop import IOLoop
# Calls fn for all items of iterable, saves result to a list, and
# calls callback with that list. This is most useful when fn works
# asynchronously.
def async_map(fn, iterable, callback, io_loop=None):
ioloop = io_loop or IOLoop.instance()
def loop():
try:
arg = next(iterator)
except StopIteration:
callback(result)
else:
fn(arg, save_result)
def save_result(value):
result.append(value)
ioloop.add_callback(loop)
iterator = iter(iterable)
result = []
ioloop.add_callback(loop)
import functools
import tornado.ioloop
from async_map import async_map
ioloop = tornado.ioloop.IOLoop.instance()
def start():
async_map(do_something, [1, 2, 3, 4], done)
def do_something(i, callback):
# A simple example of doing something asynchronously.
ioloop.add_callback(functools.partial(callback, i + 1))
def done(results):
assert results == [2, 3, 4, 5]
ioloop.stop()
start()
ioloop.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment