【问题标题】:Is it possible to create a function in Python similar to the JS interval? [duplicate]是否可以在 Python 中创建类似于 JS 区间的函数? [复制]
【发布时间】:2019-10-25 19:02:49
【问题描述】:

是否可以像在 JS 中使用 setTimer 那样在 Python 中以预定义的时间间隔异步执行代码?

例如,函数 example() 将在 Python 中每 60 秒调用一次。我该怎么做?

【问题讨论】:

  • 我认为你应该能够使用线程来做到这一点。
  • 运行一个执行循环的线程,该循环调用example(),然后调用os.sleep(60)
  • 也许这会有所帮助 stackoverflow.com/q/2697039/2776525 很棒的用户名 btw

标签: javascript python


【解决方案1】:
import threading

def printit():
  threading.Timer(5.0, printit).start()
  print "Hello, World!"

printit()

答案来自:Run certain code every n seconds

【讨论】:

    猜你喜欢
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多