Python-每n秒运行某些代码

有没有办法例如Hello World!每n秒打印一次?例如,程序将遍历我拥有的所有代码,然后经过5秒(带有time.sleep()),它将执行该代码。我将使用它来更新文件,但不打印Hello World。

例如:

startrepeat("print('Hello World')", .01) # Repeats print('Hello World') ever .01 seconds

for i in range(5):

print(i)

>> Hello World!

>> 0

>> 1

>> 2

>> Hello World!

>> 3

>> Hello World!

>> 4

回答:

import threading

def printit():

threading.Timer(5.0, printit).start()

print "Hello, World!"

printit()

# continue with the rest of your code

以上是 Python-每n秒运行某些代码 的全部内容, 来源链接: utcz.com/qa/409612.html

回到顶部