-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlr2.py
More file actions
55 lines (49 loc) · 1.48 KB
/
lr2.py
File metadata and controls
55 lines (49 loc) · 1.48 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
import concurrent.futures
import logging
import queue
import random
import threading
import time
import numpy as np
class ProducerThread(threading.Thread):
def run(self):
global queue
cnt = 0
while True:
if cnt > 7:
return
arr = np.random.randint(100, size=10)
logging.info("Producer got : %s", arr)
condition.acquire()
queue.append(arr)
condition.notify()
condition.release()
time.sleep(0.1)
cnt += 1
class ConsumerThread(threading.Thread):
def run(self):
global queue
cnt = 0.0
while True:
if cnt > 7:
return
condition.acquire()
if not queue:
print("Nothing in queue, consumer will wait.")
condition.wait()
print("Producer added something to queue - consumer will stop waiting.")
arr = queue.pop(0)
array = np.array(arr)
print("Consumed", (array[1:] + array[:-1]).max())
logging.info("Consumer received event. Exiting")
condition.release()
time.sleep(0.1)
cnt += 1
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
datefmt="%H:%M:%S")
condition = threading.Condition()
queue = []
ProducerThread().start()
ConsumerThread().start()