Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created January 22, 2015 15:44
Show Gist options
  • Save 1995eaton/35f47916be3c4a2a1269 to your computer and use it in GitHub Desktop.
Save 1995eaton/35f47916be3c4a2a1269 to your computer and use it in GitHub Desktop.
Simple Threading decorator in python
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)
@AndreMiras
Copy link

+1 for the queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment