通过 websockets 发送 popen 的输出

编程入门 行业动态 更新时间:2024-10-27 00:28:22
本文介绍了通过 websockets 发送 popen 的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用带有 fgets 的 popen 异步读取 tcpdump 的输出.

I'm using popen with fgets to read the output of tcpdump asynchronously.

下面的代码应该在命令行中运行,而不是使用 apache 并在浏览器中查看.

$handle = popen('tcpdump -nnX', 'r');

while (true) {
    $output = fgets($handle);
    print $output . "\n";
}

当我尝试通过 websockets 输出此信息时出现问题.

The problem arises when I try to output this information via websockets.

Websockets 还使用无限循环(用于管理其套接字、滴答和消息).

Websockets also use an infinite loop (for managing its sockets, ticks, and messages).

它看起来像:

while (true) {
    @socket_select($read,$write,$except,1);
    foreach ($read as $socket) {
        if ($socket == $this->master) {
            $client = socket_accept($socket);
...

我使用 $websocket->sendToAll($message); 通过 websocket 发送数据.

I send data through the websocket with $websocket->sendToAll($message);.

我不能一个接一个地放置 while 循环,因为它只会运行我首先放置的循环,while (true) { A() };while (true) { B() }; B() 永远不会被调用

I can't put the while loops one after the other because it will only run whichever loop I put first, while (true) { A() }; while (true) { B() }; B() will never be called

我无法合并 while 循环,因为 websockets 减慢了 popen 的读取速度,反之亦然.while (true) { A();B();} 如果 B 需要很长时间才能完成,A 的运行速度会很慢.

I can't merge the while loops, because the websockets slows down the reading of popen, and vise versa. while (true) { A(); B(); } if B is taking a long time to finish, A will be slow to run.

在这种情况下我该怎么办?我对线程、分叉脚本之间的通信或其他任何想法持开放态度.

What can I do in this situation? I'm open to the idea of threads, communication between forked scripts, or anything else.

推荐答案

这是 Producer-Consumer 问题的经典场景.只是你有两个.您可以分解问题以更轻松地理解它.

This is the classic scenario for Producer-Consumer problem. It's just that you've got two of them. You can break down the problem to understand it easier.

WebSocket Consumer:此代码将通过 WebSocket 发送数据.您可以将其视为一个单独的线程,其中数据从 Q1(只是一个名称)出列并发送.

WebSocket Consumer: This code will send data through WebSocket. You can consider this a separate thread in which data is dequeued from Q1 (just a name) and sent.

WebSocket Producer:一旦某些数据到达 WebSocket 门,它就会被排入缓冲区.只是这和上面的队列不一样.我们将其命名为 Q2.这也需要是一个单独的线程,一旦将数据放入队列并向适当的消费者发送信号,该线程就会进入睡眠状态.

WebSocket Producer: Once some data arrives at at the WebSocket gate, it is enqueued into a buffer. It's just that this is not the same queue as above. Let's name it Q2. This needs to be a separate thread as well, and this thread goes to sleep once it enqueues the data and signals the appropriate consumer.

HDD Consumer:此代码与 WebSocket Consumer 的作用相同,唯一的区别是它将数据存储在硬盘而不是 WebSocket 上.它将拥有自己的线程并与 Q2 一起使用.

HDD Consumer: This code will do the same as WebSocket Consumer, the only difference is that it will store the data on a hard disk instead of WebSocket. It will have its own thread and works with Q2.

HDD Producer:我相信你能猜到这是做什么的.此代码将从硬盘读取数据并将其放入Q1 队列.与所有生产者一样,它需要向消费者发出信号,通知他们队列中有新项目.

HDD Producer: I'm sure you can guess what this does. This code will read data off the hard disk and put it in Q1 queue. Like all the producers it needs to signal its consumers informing them of a new item in queue.

现在回到你的代码,PHP 不适合多线程编程,即使 完全有可能.这就是为什么你找不到那么多例子的原因.但如果你坚持,这里是你需要的:

Now getting back to your code, PHP is not suitable for multi-thread programming even though it's completely possible. That's why you can not find that many examples for it. But if you insist, here are what you'll need:

PHP 的线程类

PHP 的互斥类.这个类将帮助您防止多个线程同时访问相同的数据.

PHP's Mutex class. This class will help you prevent multiple threads to access the same data at the same time.

我在 PHP 中找不到的东西调用了 Signaling!它被使用告诉其他线程队列中的某些数据已准备好消费,或者换句话说,它会唤醒消费者线程当它有事情要做时.

Something call Signaling which I can not find in PHP! It is used to tell other threads that some data in queue is ready to be consumed, or in other words, it will wake up the consumer thread when it has something to do.

最后一句话是,在适当的多线程软件中,您不会使用 sleep 功能来降低系统负载/防止系统崩溃.多线程编程就是线程之间的信号和对话.

Final word is that in a proper multi thread software you won't be using sleep function to lower system's load / preventing system crash. Multi-thread programming is all about signaling and conversation between threads.

这篇关于通过 websockets 发送 popen 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 05:23:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1129050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:websockets   popen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!