-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadHandler.py
More file actions
29 lines (20 loc) · 796 Bytes
/
ThreadHandler.py
File metadata and controls
29 lines (20 loc) · 796 Bytes
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
import time
import _thread
# By using a global variable that
THREAD_KILL_SWITCH = dict()
# THREAD_KILL_SWITCH[thread_id] = False --> Thread continues executing
# THREAD_KILL_SWITCH[thread_id] = True --> Thread will exit next time
def startExampleThread(thread_id):
global THREAD_KILL_SWITCH
THREAD_KILL_SWITCH[thread_id] = False
print("Starting thread {}".format(thread_id))
_thread.start_new_thread(exampleThread, (thread_id, ))
def stopExampleThread(thread_id):
global THREAD_KILL_SWITCH
THREAD_KILL_SWITCH[thread_id] = True
print("Stopping thread {}".format(thread_id))
def exampleThread(thread_id):
global THREAD_KILL_SWITCH
while not THREAD_KILL_SWITCH[thread_id]:
print("This thread has id {}".format(thread_id))
time.sleep(2)