Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何从线程中退出主线程_Python 3.x_Multithreading - Fatal编程技术网

Python 3.x 如何从线程中退出主线程

Python 3.x 如何从线程中退出主线程,python-3.x,multithreading,Python 3.x,Multithreading,我有一个运行discord机器人的主脚本。但我希望它在一天中的某个特定时间退出。所以我想做一个线程,检查时间并杀死主线程 now = datetime.datetime.now().time() def time_checker(): while True: if now.hour == 19: thread.interrupt_main() thread = threading.Thread(target=time_checker, dae

我有一个运行discord机器人的主脚本。但我希望它在一天中的某个特定时间退出。所以我想做一个线程,检查时间并杀死主线程

now = datetime.datetime.now().time() 

def time_checker():
    while True:
        if now.hour == 19:
            thread.interrupt_main()

thread = threading.Thread(target=time_checker, daemon=True)
thread.start()

有办法做到这一点吗?

这段代码是如何运行的?为什么不直接退出
时间检查器
,它将结束
线程
线程。一旦退出,除非有什么东西阻止它,否则如果没有更多的代码运行,你的程序也会退出。这是我脚本的开头。这部分之后是一个循环。我想用这个线程退出这个循环。你可以调用
exit
或类似的函数;虽然这些都有缺点,如果你需要在退出之前做任何清理。退出或退出只是关闭线程,而不是主线程。