如何制作从 python3 退出的计时器
how to make timer for quiting from python3
我有一个脚本,但它不能正常工作,所以在 bash 中,我让脚本进入 while 循环,我希望我的脚本可以在一段时间后自行关闭,我尝试使用 threading.timer
但我的代码不会 运行 quit()
或 exit()
命令,有人可以帮我吗?
#!/usr/bin/env python3
import threading
from time import sleep
def qu():
print("bye")
exit()
t=threading.Timer(5.0,qu)
t.start()
while(True):
sleep(1)
print("hi")
您可以使用 os._exit
函数代替 exit()
获取代码如下:
#!/usr/bin/env python3
import threading
import os
from time import sleep
def qu():
print("bye")
os._exit(0)
t=threading.Timer(5.0,qu)
t.start()
while(True):
sleep(1)
print("hi")
无论如何,我建议您检查一下 this question,因为它与您的相似。
我有一个脚本,但它不能正常工作,所以在 bash 中,我让脚本进入 while 循环,我希望我的脚本可以在一段时间后自行关闭,我尝试使用 threading.timer
但我的代码不会 运行 quit()
或 exit()
命令,有人可以帮我吗?
#!/usr/bin/env python3
import threading
from time import sleep
def qu():
print("bye")
exit()
t=threading.Timer(5.0,qu)
t.start()
while(True):
sleep(1)
print("hi")
您可以使用 os._exit
函数代替 exit()
获取代码如下:
#!/usr/bin/env python3
import threading
import os
from time import sleep
def qu():
print("bye")
os._exit(0)
t=threading.Timer(5.0,qu)
t.start()
while(True):
sleep(1)
print("hi")
无论如何,我建议您检查一下 this question,因为它与您的相似。