我正在编写一个包含2个子进程的python脚本。主要逻辑发生在一个进程中,而另一个进程等待一段时间,然后即使该逻辑未完成也杀死该主要进程。
我了解到,调用os_exit(1)会停止解释器,因此整个脚本会自动终止。我已经使用它,如下所示:
java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
计时器进程调用os._exit,但是脚本仍然等待主进程打印import os
from multiprocessing import Process, Lock
from multiprocessing.sharedctypes import Array
# Main process
def main_process(shared_variable):
shared_variable.value = "mainprc"
time.sleep(20)
print("Task finished normally.")
os._exit(1)
# Timer process
def timer_process(shared_variable):
threshold_time_secs = 5
time.sleep(threshold_time_secs)
print("Timeout reached")
print("Shared variable ",shared_variable.value)
print("Task is shutdown.")
os._exit(1)
if __name__ == "__main__":
lock = Lock()
shared_variable = Array('c',"initial",lock=lock)
process_main = Process(target=main_process, args=(shared_variable))
process_timer = Process(target=timer_process, args=(shared_variable))
process_main.start()
process_timer.start()
process_timer.join()
后退出。
如何使它退出定时器进程,从而关闭整个程序(包括主进程)?
谢谢。