管道中的非阻塞 I/O

发布于 2024-12-13 12:33:17 字数 396 浏览 5 评论 0原文

我有一个实时收集数据的进程和另一个绘制数据的进程。这两个进程通过管道连接,数据采集进程为数据绘图进程提供数据。

我更看重数据采集部分的速度而不是绘图部分的可靠性。快速略读显示管道中的默认行为是用于写入和如果伙伴进程较慢,则读取管道末端会表现出阻塞行为。这很糟糕,因为数据采集过程可以等待绘图过程。

有没有办法让 shell 管道成为非阻塞的,就像 C 的 O_NONBLOCK 一样?我不在乎一个数据点是否没有被绘制,因为它被新的点覆盖...

编辑:实际上,我认为管道缓冲区足够大,可以容纳数据采集过程的输出,而无需立即需要绘图部分处理它。

I have one process that's gathering data in real time and another process that's plotting it. The two processes are connected by a pipe, with the data acquisition process feeding the data plotting process.

I value the speed of the data acquisition part more than the reliability of the plotting part. A quick skim shows that the default behavior in a pipe is for the writing and reading ends of a pipe to exhibit blocking behavior, if the partner process is slower. This is bad because the data acquisition process can wait on the plotting process.

Is there a way to have a shell pipe be nonblocking, a la C's O_NONBLOCK? I don't care if one data point doesn't get plotted because it gets overwritten by a newer point...

EDIT: Actually, I think the pipe buffer is large enough to hold data acquisition process's output without the plotting part immediately needing to process it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2024-12-20 12:33:17

如果数据采集过程需要不受绘图过程的限制,则需要与连接两者的管道不同的结构 - 或者您需要中间的额外过程,该过程可以丢弃尚未发送到绘图仪的旧数据点。

从概念上讲:

+-------------+    +------------+    +-------+
| Acquisition |--->|Holding Tank|--->|Plotter|
+-------------+    +------------+    +-------+

“存储罐”可以在其输入和输出描述符上使用 select() 或类似的调用,并且可以保存有限数量的数据点。当采集过程中到达新点时,如果其存储槽已满,则可以丢弃最旧的点并在其位置添加新点。如果绘图仪输入中有空间,它可以将新的数据点写入绘图仪。同时,采集过程可以全力运行(只要储罐永远不会阻塞),绘图仪也可以全力运行。这三个过程都是通过管道连接的。没有任何管道以非阻塞操作运行。

If the data acquisition process needs to run unfettered by the plotting process, you need a different structure than a pipe connecting the two - or you need an extra process in the middle that can discard old data points that have not yet been sent to the plotter.

Conceptually:

+-------------+    +------------+    +-------+
| Acquisition |--->|Holding Tank|--->|Plotter|
+-------------+    +------------+    +-------+

The 'holding tank' can be using select() or a similar call on its input and output descriptors, and can hold a finite quantity of data points. When a new point arrives from the acquisition process, if its holding tank is full, it can discard the oldest point and add the new one in its place. If there is space in the plotters input, it can write a new data point to the plotter. Meanwhile, the acquisition process can run flat out (as long as the holding tank never blocks it), and the plotter can run flat out too. These three processes are all connected by pipes. None of the pipes is run as a non-blocking operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文