forked from cms-patatrack/patatrack-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaded.py
More file actions
21 lines (17 loc) · 768 Bytes
/
threaded.py
File metadata and controls
21 lines (17 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# see https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python/14331755#14331755
def threaded(f, daemon=False):
import threading
import queue
def wrapper(q, *args, **kwargs):
'''this function calls the decorated function and puts the result in a queue'''
ret = f(*args, **kwargs)
q.put(ret)
def wrap(*args, **kwargs):
'''this is the function returned from the decorator. It fires off wrapper
in a new thread and returns the thread object with the result queue attached'''
q = queue.Queue()
t = threading.Thread(target=wrapper, args = (q,) + args, kwargs = kwargs)
t.daemon = daemon
t.result = q
return t
return wrap