下面是代码结构,我的问题是ctrl+c
无法终止程序,请老哥们看下有什么办法,搜了很多比如 signal,try 等方法都不适用
import time
import threading
class Task(threading.Thread):
def __init__(self, *args, **kwargs):
super(Task, self).__init__(*args, **kwargs)
self.daemon = True
def task1(self):
while True:
print("starting task1")
time.sleep(5)
print("task1 completed!")
def task2(self):
while True:
print("starting task2")
time.sleep(15)
print("task2 completed!")
def run(self):
try:
thread1 = threading.Thread(target=self.task1)
thread2 = threading.Thread(target=self.task2)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
except KeyboardInterrupt:
print("KeyboardInterrupt has been caught.")
if __name__ == "__main__":
task = Task()
task.run()
1
ipwx 2021-09-28 10:59:51 +08:00 1
1. time.sleep => condition variable.wait(timeout)
2. self.running = False 3. while not self.running 多线程打断不是正确的写法。正确的写法永远是让线程自己退出。 |
2
rationa1cuzz 2021-09-28 11:11:47 +08:00
你 KeyboardInterrupt 把 ctrl+c 操作 try 捕捉,ctrl+c 自然无法终止程序,在外部终止只能通过 kill 或者 signal 9 终止,非则就只能在程序内部做处理,但是你 while true,不会停下来一直跑的。
|
3
sudoy OP |
4
plko345 2021-09-28 11:34:20 +08:00 via Android
你别捕获 ctrl c 呀
|
5
rationa1cuzz 2021-09-28 13:49:44 +08:00 1
兄弟,这两行代码就是会捕捉你的 ctrl+c 的操作啊
try: except KeyboardInterrupt: 你用这代码的目的是啥? 其次你用 join 会一直等待子线程跑完主进程才会关闭,你要是想 ctrl+c 终止,可以不加 join()方法,或者设置线程为 thread1.daemon =True https://docs.python.org/3/library/threading.html?highlight=threading#module-threading |
6
ipwx 2021-09-28 13:52:55 +08:00 1
|
7
sudoy OP @rationa1cuzz 我是想 ctrl+c 以后马上结束所有线程。最后换成用 multiprocessing 实现了我的需求。感谢回复!👍
|
8
rationa1cuzz 2021-09-28 14:11:48 +08:00
|
9
ipwx 2021-09-28 15:02:01 +08:00 1
time.sleep 用楼主你的代码不容易 interrupt,ctrl + c 还要等。而且 Task 这个对象完全没必要做成 Thread 。给你个例子,Ctrl+C 可以直接立刻打断,并且最后一个 task1 completed 和 task2 completed 是会输出的:
https://gist.github.com/haowen-xu/ddc254bdf251fd2051e39874776e80ba 输出: starting task1 starting task2 task1 completed! starting task1 task2 completed! starting task2 task1 completed! starting task1 task2 completed! starting task2 ^CKeyboardInterrupt has been caught. task2 completed! task1 completed! |
12
ykk 2021-09-28 16:03:22 +08:00
需要在进程中增加退出条件
|