There are two problems.
One is how to terminate a thread in another thread by using _thread module?
Another problem is how to interrupt an infinite loop executed by exec in a thread? like this:
Code: Select all
import _thread
import time
def task1():
print('task1 running')
exec('while True:\n print("task1 ~~")\n time.sleep(1)',globals())
def task2():
print("task2 running")
while True:
print("task2 !")
time.sleep(1)
_thread.start_new_thread(task1,())
_thread.start_new_thread(task2,())
Why can Not I stop it?