python中的非阻塞事件调度 [英] Non blocking event scheduling in python

查看:81
本文介绍了python中的非阻塞事件调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 python 中安排一个函数每 xx 毫秒执行一次,而不阻塞其他事件/不使用延迟/不使用睡眠?

在 Python 中每隔 x 秒重复执行一个函数的最佳方法是什么? 解释了如何使用 sched 模块执行此操作,但该解决方案会在等待时间(如睡眠)内阻止整个代码执行.>

像下面给出的简单调度是非阻塞的,但调度只能工作一次 - 重新调度是不可能的.

from threading import Timer定义你好():打印你好,世界"t = threading.Timer(10.0, 你好)t.start()

我在安装了 Raspbian 的 Raspberry pi 中运行 python 代码.有没有办法以非阻塞方式调度函数或使用操作系统的某些功能"触发它?

解决方案

您可以通过在回调函数中启动另一个 Timer 来重新安排"事件:

导入线程定义你好():t = threading.Timer(10.0, 你好)t.start()打印你好,世界"t = threading.Timer(10.0, 你好)t.start()

Is it possible to schedule a function to execute at every xx millisecs in python,without blocking other events/without using delays/without using sleep ?

What is the best way to repeatedly execute a function every x seconds in Python? explains how to do it with sched module, but the solution will block the entire code execution for the wait time(like sleep).

The simple scheduling like the one given below is non blocking, but the scheduling works only once- rescheduling is not possible.

from threading import Timer
def hello():
    print "hello, world" 

t = threading.Timer(10.0, hello)
t.start() 

I am running the python code in Raspberry pi installed with Raspbian.Is there any way to either schedule the function in non blocking way or trigger it using 'some features' of the os?

解决方案

You can "reschedule" the event by starting another Timer inside the callback function:

import threading

def hello():
    t = threading.Timer(10.0, hello)
    t.start()
    print "hello, world" 

t = threading.Timer(10.0, hello)
t.start() 

这篇关于python中的非阻塞事件调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆