Python 中的实时中断

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23170318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:23:33  来源:igfitidea点击:

Real-time interrupts in Python

pythonperformancepython-2.7timeinterrupt

提问by Stack Player

I have a Python 2.7 program running an infinite while loop and I want to incorporate a timer interrupt. 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.

我有一个运行无限 while 循环的 Python 2.7 程序,我想合并一个计时器中断。我的目标是在循环中的某个点启动一个计时器,当 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 exactlywhen 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. Using Timer did not work either.

此外,我需要它为 1 个以上的计数器(可能高达 100)工作,但我不希望中断相互中断。使用计时器也不起作用。

I know this can be done using timer interrupts in assembly but how can I do that in python?

我知道这可以在汇编中使用定时器中断来完成,但我如何在 python 中做到这一点?

采纳答案by Roberto Reale

If a single event is to be handled, then the easiest way is to use the signalframework 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. Hereis a pointer to the official documentation. Please be aware, though, that in multi-threaded environments schedhas limitations with respect to thread-safety.

然而,如果我们需要一个成熟的调度器,那么我们必须求助于另一个模块:sched. 是指向官方文档的指针。但请注意,在多线程环境sched中,线程安全有限制

Another option is the Advanced Python Scheduler, which is not part of the standard distribution.

另一种选择是Advanced Python Scheduler,它不是标准发行版的一部分。

回答by jfs

You can't get real-timewithout 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 e.g., 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.

它实现了您的“我也不希望中断相互中断”,因为在前一个回调完成之前不会调用下一个回调。