-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode16.py
More file actions
65 lines (47 loc) · 1.76 KB
/
code16.py
File metadata and controls
65 lines (47 loc) · 1.76 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
# code16.py
import threading
import time
import queue
class Producer(threading.Thread):
def __init__(self, que):
super().__init__()
self.que = que
def run(self):
with open('raw_numbers.txt') as file:
for line in file:
values = tuple([int(l) for l in line.strip().split(',')])
self.que.put(values)
print('[PRODUCER] The queue has {} elements.'.format( \
self.que.qsize()))
# Simulates a slower process
time.sleep(0.001)
def consumer(que):
with open('processed_numbers.txt', 'w') as file:
while True:
# A try/except clause is used in order to stop
# the consumer once there is no elements left in the
# queue. If not for this, the consumer would be executing
# for ever
try:
# If no elements are left in the queue, an Empty
# exception is raised
numbers = que.get(False)
except queue.Empty:
break
else:
file.write('{}\n'.format(sum(numbers)))
que.task_done()
# qsize() returns the queue size
print('[CONSUMER] The queue now has {} elements.'.format( \
que.qsize()))
# Simulates a complex process. If the consumer was faster
# than the producer, the threads would end abruptly
time.sleep(0.005)
if __name__ == '__main__':
q = queue.Queue()
# a producer is created and executed
p = Producer(q)
p.start()
# a consumer thread is created and executed with the same queue
c = threading.Thread(target=consumer, args=(q,))
c.start()