服务器,它将杀死并再次运行无限循环

时间:2017-08-04 08:24:36

标签: python loops server

我需要创建简单的服务器,它将运行某种infite循环,例如:

try:
  while True:
            time.sleep(1)
            print ("I'm Running",time.clock())

我需要服务器在一段时间后杀死循环,然后再次运行它,直到我中断所有内容。你能帮我吗?可能会使用子流程

1 个答案:

答案 0 :(得分:1)

这个怎么样:

import time

while True:
    try:
        time.sleep(1)
        print("I'm Running",time.clock())
    except KeyboardInterrupt:
        print("Don't stop me now! I'm having such a good time...")
        time.sleep(x) # instead of x put the number of
                      # seconds you want before it will start again