Created
January 22, 2015 15:44
-
-
Save 1995eaton/35f47916be3c4a2a1269 to your computer and use it in GitHub Desktop.
Simple Threading decorator in python
This file contains 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 threading import Thread | |
from queue import Queue | |
def threaded(fn): | |
def wrap(queue, *args, **kwargs): | |
queue.put(fn(*args, **kwargs)) | |
def call(*args, **kwargs): | |
queue = Queue() | |
job = Thread(target=wrap, args=(queue,) + args, | |
kwargs=kwargs) | |
job.start() | |
return queue | |
return call | |
if __name__ == '__main__': | |
from time import sleep | |
@threaded | |
def f(x, y): | |
print('ran job #{}'.format(x)) | |
return 2 ** y | |
jobs = [job.get() for job in [f(i, i) for i in range(10)]] | |
print(jobs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would you mind elaborating what the queue is for?