简体   繁体   English

Python中的实时中断

[英]Real-time interrupts in Python

I have a Python 2.7 program running an infinite while loop and I want to incorporate a timer interrupt. 我有一个运行无限循环的Python 2.7程序,我想要合并一个定时器中断。 What I aim to do is to set off a timer at some point in the loop, and when 5 seconds have elapsed I want the code to branch to a specific part of the while loop. 我的目的是在循环中的某个点设置一个计时器,当经过5秒后,我希望代码分支到while循环的特定部分。

What I have been doing so far is the following: in each iteration of the while loop I am checking how much time has elapsed when I reach that point in the code using 到目前为止我一直在做的是:在while循环的每次迭代中,我正在检查当我使用代码中的那个点时经过了多长时间

time.clock()

and if the difference exceeds 5 I run the chunk of code I mean to run 如果差异超过5,我运行我想要运行的代码块

However that way 7 seconds might pass before I evaluate the time, it will be >5sec but I want to go there exactly when 5 seconds pass 然而,在我评估时间之前,这种方式可能会持续7秒,但是它会超过5秒,但是我希望在5秒钟后完全通过

Also, I need this to work for more than 1 counter (possibly up to a 100) but I do not want the interrupts to interrupt each other. 此外,我需要这个工作超过1个计数器(可能高达100)但我不希望中断相互中断。 Using Timer did not work either. 使用Timer也不起作用。

I know this can be done using timer interrupts in assembly but how can I do that in python? 我知道这可以在程序集中使用定时器中断来完成,但我怎么能在python中做到这一点?

If a single event is to be handled, then the easiest way is to use the signal framework which is a standard module of Python . 如果要处理单个事件,那么最简单的方法是使用signal框架,它是Python标准模块

However, if we need a fully-fledged scheduler, then we have to resort to another module: sched . 但是,如果我们需要一个完全成熟的调度程序,那么我们必须求助于另一个模块: sched Here is a pointer to the official documentation. 是指向官方文档的指针。 Please be aware, though, that in multi-threaded environments sched has limitations with respect to thread-safety . 但请注意,在多线程环境中, sched 在线程安全方面存在局限性

Another option is the Advanced Python Scheduler , which is not part of the standard distribution. 另一个选项是Advanced Python Scheduler ,它不是标准发行版的一部分。

You can't get real-time without special hardware/software support. 没有特殊的硬件/软件支持,您无法实时获得。 You don't need it in most cases ( do you need to control giant robots? ). 在大多数情况下不需要它( 你需要控制巨型机器人吗? )。

How to delay several function calls by known numbers of seconds depends on your needs eg, if a time it takes to run a function is negligible compared to the delay between the calls then you could run all functions in a single thread : 如何以已知的秒数延迟多个函数调用取决于您的需要,例如,如果运行函数所花费的时间与调用之间的延迟相比可以忽略不计,那么您可以在单个线程中运行所有函数

#!/usr/bin/env python
from __future__ import print_function
from Tkinter import Tk

root = Tk()
root.withdraw() # don't show the GUI window
root.after(1000, print, 'foo') # print foo in a second
root.after(0, print, 'bar') # print bar in a jiffy
root.after(2000, root.destroy) # exit mainloop in 2 seconds
root.mainloop()
print("done")

It implements yours "I do not want the interrupts to interrupt each other either" because the next callback is not called until the previous one is complete. 它实现了你的“我不希望中断相互中断”,因为下一个回调在前一个回调完成之前不会被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM