2017-08-14 92 views
0

我有一個腳本,它不能正常工作,所以在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") 

回答

1

您可以使用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,因爲它是與你相似。

+0

tnx,用於調用我的腳本我使用'while [true];做python3 script.py; sleep 5;'done;'有沒有什麼辦法可以通過pytion自己回想我的腳本? –

+0

@SaebMolaee歡迎您從python調用任何腳本,您可以使用[subprocess module](https://docs.python.org/3/library/subprocess.html) –