-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancel_event.py
More file actions
50 lines (37 loc) · 1.29 KB
/
cancel_event.py
File metadata and controls
50 lines (37 loc) · 1.29 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
import threading
import time
from temporal_scheduler import TemporalScheduler
class eventAction(threading.Thread):
"""
Implements a test thread waiting for an event.
Class records time of each activation for further analysis
"""
def __init__(self, thread_id, sync):
threading.Thread.__init__(self)
self.thread_id = thread_id
self.sync = sync
def run(self):
# wait for the event for max 5 seconds.
if self.sync.wait(5):
print("Thread %s: event fired" % self.thread_id)
else:
print("*** Thread %s: event timed out" % self.thread_id)
self.sync.clear()
ts = TemporalScheduler()
sync_event = threading.Event()
print("Starting worker thread")
thread1 = eventAction(1, sync_event)
thread1.start()
print("Scheduling the event to start in 4 seconds\n")
ev_id = ts.schedule_task(sync_event, delta_t = 4)
print("Waiting for 3 seconds\n")
time.sleep(3)
print("Cancelling event")
ts.cancel_task(ev_id)
print("Sleeping for another 3 seconds.")
print("In 2 seconds you should see a message from the thread about event timeout\n")
time.sleep(3)
# Start the threads and wait until they complete
thread1.join()
print("\nBy now the event should have timed out, and the thread completed. Time to shut down")
ts.shutdown()