Created
April 7, 2011 13:20
-
-
Save akheron/907770 to your computer and use it in GitHub Desktop.
Asynchronous map for Tornado's ioloop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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