【问题标题】:how to make timer for quiting from python3如何制作退出python3的计时器
【发布时间】:2017-08-14 15:04:15
【问题描述】:

我有一个脚本,但它不能正常工作,所以在 bash 中我让脚本进入 while 循环,我希望我的脚本可以在一段时间后自行关闭,我尝试使用 threading.timer 但我的代码无法运行 @987654322 @ 或 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")

【问题讨论】:

    标签: python python-multithreading python-3.6


    【解决方案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,因为它与您的相似。

    【讨论】:

    • @SaebMolaee 不客气,要从 python 调用任何脚本,您可以使用subprocess module
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多