-
Notifications
You must be signed in to change notification settings - Fork 51
/
thread_pools.py
59 lines (41 loc) · 1.18 KB
/
thread_pools.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
#!/usr/bin/env python3
from myThread import MyThread
from queue import Queue
from random import randint
from sys import argv
from time import sleep
def writeQ(queue):
print("producing object for Q...", end=" ")
queue.put("xxx", 1)
print("size now:", queue.qsize())
def readQ(queue):
if queue.get(1):
print("consumed object from Q... size now", queue.qsize())
else:
print("Q currently empty...")
def writer(queue, loops):
for i in range(loops):
writeQ(queue)
sleep(randint(1, 3))
def reader(queue, loops):
for i in range(loops):
readQ(queue)
sleep(randint(5, 7))
def main():
nloops = int(argv[1])
q = Queue(32)
nconsumers = int(argv[2])
if nloops < nconsumers:
print("Too many threads for %s loops. Use less threads." % nloops)
return
threads = [MyThread(writer, (q, nloops), writer.__name__)]
for i in range(nconsumers):
t = MyThread(reader, (q, nloops//nconsumers), 'consumer %s' % i)
threads.append(t)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print("all DONE")
if __name__ == "__main__":
main()