-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithreadedWriteTest_0.py
57 lines (47 loc) · 1.42 KB
/
multithreadedWriteTest_0.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
#multithreadedWriteTest_0
import multiprocessing
import time
from queue import LifoQueue
def computeX(index,item,stackSize):
_start = time.time()
output = "index = " + str(index) + "; "
for x in range(0, 10000000):
pass
resultQueue.put(output)
print("Process {0} took {1} seconds to compute item {2} from stack which is now size: {3}".format(index,
(time.time() - _start), item, stackSize))
def printFromQueue():
while True:
queueOut = resultQueue.get()
print(queueOut, file=fileOut)
if resultQueue.empty():
#this must be called before join_thread:
resultQueue.close()
break
#https://docs.python.org/3/library/queue.html
#->
#https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Queue
resultQueue = multiprocessing.Queue()
# max threads to process
cpu_count = multiprocessing.cpu_count()
processArray = []
#https://www.geeksforgeeks.org/stack-in-python/
stack = LifoQueue()
for i in range(100):
stack.put(i)
while not stack.empty():
for index in range(cpu_count):
if stack.empty():
break
item = stack.get()
process = multiprocessing.Process(target=computeX, args=(index, item, stack.qsize(),))
process.start()
processArray.append(process)
for j in range(cpu_count):
processArray[j].join()
#https://docs.python.org/3/tutorial/inputoutput.html
#fileOut = open('file.txt', 'wt')
with open('file.txt', 'wt') as fileOut:
printFromQueue()
#fileOut.close()
resultQueue.join_thread()