-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.py
38 lines (32 loc) · 1.06 KB
/
event.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
"""
Example of a worker thread that wakes up in response
to an Event object being set.
MCS 260 Fall 2020 Lecture 42 - Emily Dumas
"""
import threading
import time
# The integer that the worker thread
# is to operate on
workdata = 0
# Event to indicate a recalculation
# is needed
workdata_updated = threading.Event()
def worker_run():
"""Main function of the worker thread"""
while True:
# Wait for a signal that recalculation is needed
workdata_updated.wait()
# Acknowledge the signal so it doesn't trigger
# another recalculation (after this one)
workdata_updated.clear()
x = workdata
print("worker: Starting work on square of {}".format(x))
time.sleep(2)
print("worker: Calculated {}**2 = {}".format(x,x*x))
worker = threading.Thread(target=worker_run,daemon=True)
worker.start()
for i in range(20):
time.sleep(0.5)
workdata = i
print("main: Updated workdata to {}, signaling worker thread to compute.".format(workdata))
workdata_updated.set()