-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithreadedWriteTest.py
79 lines (62 loc) · 1.88 KB
/
multithreadedWriteTest.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#multithreadedWriteTest
#starting here:
#https://stackoverflow.com/questions/11983938/python-appending-to-same-file-from-multiple-threads
#Problem: How to print using multithreading
#Solution: Use 1 thread for printing
#Bug: There are 4 processing threads (0-3), but only thread 0 gets printed
#Next step: try with import multiprocessing
import queue
import threading
import codecs
import multiprocessing
class PrintThread(threading.Thread):
def __init__(self, ptQueue):
threading.Thread.__init__(self)
self.ptQueue = ptQueue
def printFiles(self, queueOut):
print("PrintThread printing")
print(queueOut, file=fileOut)
def run(self):
while True:
queueOut = self.ptQueue.get()
self.printFiles(queueOut)
self.ptQueue.task_done()
class ComputeThread(threading.Thread):
def __init__(self, in_queue, out_queue, index):
threading.Thread.__init__(self)
self.in_queue = in_queue
self.out_queue = out_queue
self.index = index
def run(self):
while True:
path = self.in_queue.get()
result = self.compute()
self.out_queue.put(result)
self.in_queue.task_done()
def compute(self):
print("ComputeThread computing thread #" + str(self.index))
output = "index = " + str(self.index) + "; "
for x in range(0, 100000):
output = output + str(x) + " "
return output
pathQueue = queue.Queue()
resultQueue = queue.Queue()
#https://docs.python.org/3/library/codecs.html
fileOut = codecs.open('file.txt', 'wt')
#https://docs.python.org/3/tutorial/inputoutput.html
# spawn threads to process
threadLimit = multiprocessing.cpu_count()
print("threadLimit: " + str(threadLimit))
for i in range(threadLimit):
print("starting thread #" + str(i))
t = ComputeThread(pathQueue, resultQueue, i)
t.daemon = True
t.start()
# spawn thread to print
t = PrintThread(resultQueue)
t.daemon = True
t.start()
pathQueue.put('.')
# wait for queue to get empty
pathQueue.join()
resultQueue.join()