I use _thread module to create two threads in main.py, and they are running with a "while" loop. When I want to use ctrl+C to stop these threads, they can't stop. How can I stop these thread?
The code as following.
Code: Select all
class Thread:
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None):
self.target = target
self.args = args
self.kwargs = {} if kwargs is None else kwargs
def start(self):
_thread.start_new_thread(self.run, ())
def run(self):
self.target(*self.args, **self.kwargs)
def task1():
print("run task1")
while True:
time.sleep(1)
print("task1 running")
import time
import _thread
def task2():
while True:
print("Task2 running")
time.sleep(1)
Thd1 = Thread(target=task1)
Thd2 = Thread(target=task2)
Thd1.start()
Thd2.start()