簡體   English   中英

如何使計時器從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")

無論如何,我建議您檢查這個問題,因為它與您的問題相似。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM