如何使从python3退出计时器
问题描述:
我有一个脚本,它不能正常工作,所以在bash我让脚本在while循环,我想我的脚本可以关闭自己一段时间后,我试图使用threading.timer
但我的代码不会运行quit()
或exit()
命令,任何人都可以请帮我吗?如何使从python3退出计时器
#!/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,因为它是与你相似。
tnx,用于调用我的脚本我使用'while [true];做python3 script.py; sleep 5;'done;'有没有什么办法可以通过pytion自己回想我的脚本? –
@SaebMolaee欢迎您从python调用任何脚本,您可以使用[subprocess module](https://docs.python.org/3/library/subprocess.html) –