Sep-25-2023, 11:20 PM
(This post was last modified: Sep-25-2023, 11:20 PM by deanhystad.)
The global variable belongs in script B, not script A.
napper.py is a script that that contains a function I want to interrupt.
napper.py is a script that that contains a function I want to interrupt.
import time wake_up = True def take_a_nap(): global wake_up wake_up = False print("I'm taking a nap.") while not wake_up: time.sleep(1) print("Zzzzzz") print("I'm awake")test.py is a script that uses napper.
import threading import time import napper t1 = threading.Thread(None, napper.take_a_nap) t1.start() time.sleep(5) print("Time to wake up!") napper.wake_up = True t1.join() print("Good morning")
Output:I'm taking a nap.
Zzzzzz
Zzzzzz
Zzzzzz
Zzzzzz
Time to wake up!
Zzzzzz
I'm awake
Good morning