Sre*_*zha 4 python timer scheduled-tasks nonblocking raspberry-pi
是否可以安排一个函数在python中的每个xx毫秒执行,而不阻止其他事件/没有使用延迟/不使用睡眠?
在Python中每x秒重复执行一次函数的最佳方法是什么?解释了如何使用sched模块执行此操作,但解决方案将阻止整个代码执行等待时间(如睡眠).
像下面给出的简单调度是非阻塞的,但是调度只能工作一次 - 重新安排是不可能的.
from threading import Timer
def hello():
print "hello, world"
t = threading.Timer(10.0, hello)
t.start()
Run Code Online (Sandbox Code Playgroud)
我在使用Raspbian安装的Raspberry pi中运行python代码.有没有办法以非阻塞方式安排功能或使用操作系统的"某些功能"触发它?
您可以通过Timer
在回调函数内启动另一个来"重新安排"事件:
import threading
def hello():
t = threading.Timer(10.0, hello)
t.start()
print "hello, world"
t = threading.Timer(10.0, hello)
t.start()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6360 次 |
最近记录: |