php 命令行阅读器

发布于 2024-12-28 22:14:08 字数 389 浏览 1 评论 0原文

所以我希望能够在 PHP 中执行以下操作。我从命令行调用一个脚本。

 /usr/bin/php mychildren.php

我希望脚本能够创建 2 个子进程,它们都无限期地保持活动状态。 (假设我们让它们无限循环=D),但我希望子进程偶尔对第一个进程回显你好,对第二个进程回显再见。然后,当我使用 pcntl_signal 执行信号中断(ctrl+c)时,我可以杀死 2 个子进程,一旦我验证它们被杀死,我就杀死父进程。

这还有可能吗?!我浏览了一下流媒体,我对如何让它发挥作用感到非常困惑。看起来应该可以工作,但我无法让任何东西正常工作。

快速详细信息: 2个子进程 每个子进程偶尔都会回显一些随机的东西 当我杀死父母时,孩子们就会死,一旦他们死了,父母就会死

So I want to be able to do the following in PHP. From the command line I call a script.

 /usr/bin/php mychildren.php

I want the script to be able to create 2 child processes both of which stay active indefinitely. (say we make them infinite loops =D), but I want the child processes to occasionally to echo out hello for the 1st process and goodbye for the second process. And then when I do a signal interrupt (ctrl+c) using pcntl_signal I can then kill the 2 child processes and once I have verification that they are killed then I kill the parent process.

Is this even possible?! I looked through streaming a little and I am super confuzzled as to how to get this working. Seems like it should work, but I can't get anything to work properly.

Quick details:
2 child processes
each child processes occasionally echos something random
when I kill the parent the children die, and once they are dead then the parent dies

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

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

发布评论

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

评论(1

万人眼中万个我 2025-01-04 22:14:08

虽然您可以使用 pcntl_fork 创建子进程,通常最好使用 proc_open 重新执行子进程。使用 pcntl_signal 安装信号处理程序(以终止子进程)。如果您希望子进程直接写入相同的输出,则必须实现某种 IPC 以避免同时写入。

因此,最好让两个子进程都写入主进程,并让主进程等待整行或以其他方式同步输出。

While you can use pcntl_fork to create subprocesses, oftentimes it is better to execute the subprocesses anew with proc_open. Use pcntl_signal to install signal handlers (to kill the subprocesses). If you want the child processes to directly write to the same output, you'll have to implement some kind of IPC to avoid both writing at the same time.

Therefore, it's probably better to let both subprocesses write to the main process, and let the main process wait for full lines or otherwise synchronize outputs.

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