代码之家  ›  专栏  ›  技术社区  ›  Saeb Molaee

如何制作退出python3的计时器

  •  0
  • Saeb Molaee  · 技术社区  · 8 年前

    我有一个脚本,它不能正常工作,所以在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")
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Martin Alonso    8 年前

    你可以使用 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 因为它和你的相似。