php 命令行阅读器
所以我希望能够在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然您可以使用
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 withproc_open
. Usepcntl_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.