-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_example_with_io.py
63 lines (56 loc) · 1.3 KB
/
process_example_with_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
'''
j.philipson
10:59 PM, 1/27/2021
'''
import threading
import time
#
#
#
# return_value=[]
# with concurrent.futures.ThreadPoolExecutor() as executor:
# for i in range(0,100):
# future = executor.submit(foo, 'world!')
#
# for i in range(0,100):
# return_value[i] = future.result()
# print(return_value)
#
# print('done ya')
def threading_func(f):
"""Decorator for running a function in a thread and handling its return
value or exception"""
def start(*args, **kw):
def run():
try:
th.ret = f(*args, **kw)
except:
th.exc = sys.exc_info()
def get(timeout=None):
th.join(timeout)
if th.exc:
# raise th.exc[0], th.exc[1], th.exc[2] # py2
raise th.exc[1] #py3
return th.ret
th = threading.Thread(None, run)
th.exc = None
th.get = get
th.start()
return th
return start
@threading_func
def foo(bar, val):
print('hello {}'.format(bar))
print('started!')
time.sleep(val)
print('done')
return val
th=[]
for i in range(0,5):
th.append( foo("text", i) )
try:
for i in range(0,5):
print(th[i].get())
except TypeError:
print("exception thrown ok.")