通过Python实时操作 [英] Real-time operating via Python

查看:47
本文介绍了通过Python实时操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是一个没有经验的 Python 编码员,我收集到的可能是一个相当复杂的需求.我是一名认知科学家,我需要精确的刺激显示和按钮按下检测.有人告诉我,最好的方法是使用实​​时操作,但不知道如何去做.理想情况下,每次试用时,程序都会实时运行,然后一旦试用结束,操作系统就可以回到不那么细致的状态.将有大约 56 次试验.有没有办法从我的 python 脚本中编写代码?

So I am an inexperienced Python coder, with what I have gathered might be a rather complicated need. I am a cognitive scientist and I need precise stimulus display and button press detection. I have been told that the best way to do this is by using real-time operating, but have no idea how to go about this. Ideally, with each trial, the program would operate in real-time, and then once the trial is over, the OS can go back to not being as meticulous. There would be around 56 trials. Might there be a way to code this from my python script?

(再说一次,我需要知道的是实际显示刺激的时间.实时方法可以确保在我想要的时候显示刺激,这是一种自上而下的方法.另一方面,我可以采取更自下而上的方法,如果计算机真正有机会显示它更容易知道记录.)

(Then again, all I need to know is when a stimulus is actually displayed. The real-time method would assure me that the stimulus is displayed when I want it to be, a top-down approach. On the other hand, I could take a more bottom-up approach if it is easier to just know to record when the computer actually got a chance to display it.)

推荐答案

当人们谈论实时计算时,他们的意思是从中断(最常由定时器触发)到应用程序代码处理的延迟正在运行的中断既小又可预测.这意味着控制过程可以以非常精确的时间间隔重复运行,或者在您的情况下,可以非常精确地计时外部事件.延迟的变化通常被称为抖动"——1ms 的最大抖动意味着一个重复到达的中断将有一个最多 1ms 变化的响应延迟.

When people talk about real-time computing, what they mean is that the latency from an interrupt (most commonly set off by a timer) to application code handling that interrupt being run, is both small and predictable. This then means that a control process can be run repeatedly at very precise time intervals or, as in your case, external events can be timed very precisely. The variation in latency is usually called "jitter" - 1ms maximum jitter means that an interrupt arriving repeatedly will have a response latency that varies by at most 1ms.

小"和可预测"都是相对的术语,当人们谈论实时性能时,它们可能意味着 1μs 的最大抖动(例如,为电力传输构建逆变器的人关心这种性能),或者它们可能意味着几毫秒的最大抖动.这一切都取决于应用程序的要求.

"Small" and "predictable" are both relative terms and when people talk about real-time performance they might mean 1μs maximum jitter (people building inverters for power transmission care about this sort of performance, for instance) or they might mean a couple of milliseconds maximum jitter. It all depends on the requirements of the application.

无论如何,出于以下几个原因,Python 不太可能成为这项工作的合适工具:

At any rate, Python is not likely to be the right tool for this job, for a few reasons:

  • Python 主要在桌面操作系统上运行.桌面操作系统对最大抖动施加了下限;在 Windows 的情况下,它是几秒钟.多秒事件不会经常发生,每天或每两天发生一次,如果有一个事件与您要测量的事物重合,这将是不幸的,但迟早它会发生;几百毫秒区域的抖动发生得更频繁,可能每小时一次,而几十毫秒区域的抖动相当频繁.桌面 Linux 的数字可能相似,但您可以将不同的编译时选项和补丁集应用于 Linux 内核以改善这种情况 - Google PREEMPT_RT_FULL.
  • Python 的 stop-the-world 垃圾收集器使延迟不确定.当 Python 决定它需要运行垃圾收集器时,你的程序会停止直到它完成.您可以通过仔细的内存管理和仔细设置垃圾收集器参数来避免这种情况,但根据您使用的库,您也可能不会这样做.
  • Python 内存管理的其他功能使确定性延迟变得困难.大多数实时系统避免堆分配(即 C 的 malloc 或 C++ 的 new),因为它们花费的时间是不可预测的.Python 巧妙地向您隐藏了这一点,这使得控制延迟变得非常困难.同样,使用大量现成的优秀库只会让情况变得更糟.
  • 同样,实时进程必须将其所有内存都保存在物理 RAM 中,而不是调出以进行交换.在 Python 中没有很好的方法来控制这一点,尤其是在 Windows 上运行时(在 Linux 上,您可能可以在某处调用 mlockall,但任何新的分配都会使事情变得混乱).
  • Python runs mostly on desktop operating systems. Desktop operating systems impose a lower limit on the maximum jitter; in the case of Windows, it is several seconds. Multiple-second events don't happen very often, every day or two, and you'd be unlucky to have one coincide with the thing you're trying to measure, but sooner or later it will happen; jitter in the several-hundred-milliseconds region happens more often, perhaps every hour, and jitter in the tens-of-milliseconds region is fairly frequent. The numbers for desktop Linux are probably similar, though you can apply different compile-time options and patch sets to the Linux kernel to improve the situation - Google PREEMPT_RT_FULL.
  • Python's stop-the-world garbage collector makes latency non-deterministic. When Python decides it needs to run the garbage collector, your program gets stopped until it finishes. You may be able to avoid this through careful memory management and carefully setting the garbage collector parameters, but depending on what libraries you are using, you may not, too.
  • Other features of Python's memory management make deterministic latency difficult. Most real-time systems avoid heap allocation (ie C's malloc or C++'s new) because the amount of time they take is not predictable. Python neatly hides this from you, making it very difficult to control latency. Again, using lots of those nice off-the-shelf libraries only makes the situation worse.
  • In the same vein, it is essential that real-time processes have all their memory kept in physical RAM and not paged out to swap. There is no good way of controlling this in Python, especially running on Windows (on Linux you might be able to fit a call to mlockall in somewhere, but any new allocation will upset things).

不过我有一个更基本的问题.您不会说您的按钮是物理按钮还是屏幕上的按钮.如果它在屏幕上,操作系统将在物理鼠标按钮按下和到达 Python 应用程序的事件之间施加不可预测的延迟.你将如何解释这一点?没有更准确的测量方法,你怎么知道它是否存在?

I have a more basic question though. You don't say whether your button is a physical button or one on the screen. If it's one on the screen, the operating system will impose an unpredictable amount of latency between the physical mouse button press and the event arriving at your Python application. How will you account for this? Without a more accurate way of measuring it, how will you even know whether it is there?

这篇关于通过Python实时操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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