Created
January 26, 2012 14:51
-
-
Save barbuza/1683115 to your computer and use it in GitHub Desktop.
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
class ParallelWorker(threading.Thread): | |
def __init__(self, cb, fn, args, kwargs): | |
self._cb = cb | |
self._fn = fn | |
self._args = args | |
self._kwargs = kwargs | |
super(ParallelWorker, self).__init__() | |
def run(self): | |
result = self._fn(*self._args, **self._kwargs) | |
cb = functools.partial(self._cb, result) | |
ioloop.IOLoop().instance().add_callback(cb) | |
def parallel(fn): | |
def wrapper(*args, **kwargs): | |
callback = kwargs.pop("callback") | |
worker = ParallelWorker(callback, fn, args, kwargs) | |
worker.start() | |
def decor(*args, **kwargs): | |
return gen.Task(wrapper, *args, **kwargs) | |
return decor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment