-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_demo.py
More file actions
68 lines (55 loc) · 1.75 KB
/
thread_demo.py
File metadata and controls
68 lines (55 loc) · 1.75 KB
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
64
65
66
67
68
import logging
logging.basicConfig(filename='thread_demo.log',
level=logging.DEBUG,
format='%(asctime)s.%(msecs)03d %(threadName)-16s %(levelname)-6s %(message)s',
datefmt='%H:%M:%S')
import threading
import Queue
import numpy as np
procstack = 10
def fill(outq):
logging.debug("Starting Fill thread")
for i in range(procstack):
logging.debug("fill %4i started" % (i))
outq.put(np.random.rand(1000,10000))
logging.debug("fill %4i complete" % (i))
logging.debug("sending kill to que")
outq.put("END")
def proc(inq, outq):
logging.debug("Starting Processing Thread")
i = 0
while True:
data = inq.get()
if data == "END" :
inq.put("END")
logging.debug("proc found end statement, finishing")
return
logging.debug("proc %4i started" % (i))
outq.put(np.sin(np.log(data)))
logging.debug("proc %4i complete" % (i))
i+=1
logging.debug("Finished Processing Thread")
def plot(inq):
logging.debug("Starting Plotting Thread")
for i in range(procstack):
data = inq.get()
logging.debug("plot %4i started" % (i))
m = np.mean(data)
logging.debug("plot %4i complete" % (i))
logging.debug("Finished Processing Thread")
q1 = Queue.Queue()
q2 = Queue.Queue()
f = threading.Thread(name='FillingThread', target=fill, args = (q1,))
f.daemon = True
p = threading.Thread(name='PlottingThread', target=plot, args = (q2,))
p.daemon = True
ps = []
for i in range(4):
pp = threading.Thread(name='ProcessingThread-%02i' % (i), target=proc, args = (q1,q2))
pp.daemon = True
pp.start()
ps.append(pp)
p.start()
f.start()
p.join()
logging.debug("All Done")