-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path05-4_Python-Threads.py
46 lines (35 loc) · 968 Bytes
/
05-4_Python-Threads.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
#!/usr/bin/python3
import threading
import time
interrupt_interval=5 # in Sekunden
# ACHTUNG:
# * `interrupt` ist "Shared State"!
# * ... und ist *nicht* synchronisiert!
# * -> das ist *nicht* korrekt!
#
interrupt=False
def interrupt_controller_thread():
while True:
global interrupt
time.sleep(interrupt_interval)
print("Triggering interrupt")
interrupt=True
def cpu_handle_interrupt():
global interrupt
if interrupt == True:
print("Woah I got an interrupt")
# now act on the interrupt...
interrupt = False
def cpu_thread():
while True:
# Read, Eval, (optional: Print), Loop
print("I am reading the next instruction")
print("I am evaluating the instruction")
time.sleep(1) # python threading doesn't really work
cpu_handle_interrupt()
cpu = threading.Thread( target = cpu_thread )
pic = threading.Thread( target = interrupt_controller_thread )
cpu.start()
pic.start()
cpu.join()
pic.join()