一个 PHP 脚本可以启动另一个 PHP 脚本并退出吗?
PHP 脚本如何启动另一个 PHP 脚本,然后退出,让另一个脚本继续运行?
另外,第二个脚本有什么方法可以在到达特定行时通知 PHP 脚本吗?
How can a PHP script start another PHP script, and then exit, leaving the other script running?
Also, is there any way for the 2nd script to inform the PHP script when it reaches a particular line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您不想构建 pcntl 扩展,那么一个好的替代方法是使用 proc_open()。
http://www.php.net/manual/en/function。 proc-open.php
将其与stream_select() 一起使用,以便您的PHP 进程可以休眠,直到您创建的子进程发生某些情况。
这将有效地在后台创建一个进程,而不会阻塞父 PHP 进程。 PHP 可以读取和写入 STDIN、STDOUT、STDERR。
要使浏览器完成加载(停止加载进度指示器),您可以使用 Milan Babuškov 提到的内容。
让浏览器认为HTTP请求完成的关键是向其发送内容长度。为此,您可以开始缓冲请求,然后在发送 Content-Length 标头后刷新它。
例如:
If you don't want to build the pcntl extension, then a good alternative is to use proc_open().
http://www.php.net/manual/en/function.proc-open.php
Use that together with stream_select() so your PHP process can sleep until something happens with the child process you created.
That will effectively create a process in the background, without blocking the parent PHP process. You PHP can read and write to STDIN, STDOUT, STDERR.
To make the browser complete loading (stop the load progress indicator) then you can use what Milan Babuškov mentioned.
The key to making the browser think the HTTP request is complete, is to send it the content length. To do this you can start buffering the request, then flush it after you send the Content-Length header.
eg:
您可以创建请求并在写入完成后立即关闭连接。
查看 http://drupal.org/project/httprl 中的代码,可以做到这一点(非阻塞要求)。我计划在完善这个库后将其推送到 github;可以在 drupal 之外运行的东西。这应该可以满足您的需求。
You can create a request and close the connection right after it is done being written to.
Checkout the code in http://drupal.org/project/httprl as can do this (non-blocking request). I plan on pushing this lib to github once I get it more polished; something that can be ran outside of drupal. This should do what your looking for.
以下是具体操作方法。您告诉浏览器读取输出的前 N 个字符,然后关闭连接,而您的脚本将继续运行直到完成。
Here's how to do it. You tell the browser to read in the first N characters of output and then close the connection, while your script keeps running until it's done.
您可以通过 分叉 然后调用
包括
或需要
。parent.php:
sleeper.php:
输出:
但是,分叉本质上不允许任何进程间通信,因此您必须找到其他方法来通知父级子级已到达特定行,就像您在的问题。
You can effectively achieve this by forking and then calling
include
orrequire
.parent.php:
sleeper.php:
Output:
However, forking does not inherently allow any inter-process communication, so you'd have to find some other way to inform the parent that the child has reached the specific line, like you asked in the question.
这是一个盲目的尝试:您可以尝试使用 php 的操作系统执行函数与
&
。另外,如果这不起作用,您可以尝试
编辑: nohup 是一个忽略 HUP 的 POSIX 命令( hangup) 信号,使命令在发出命令的用户注销后继续运行。按照惯例,HUP(挂断)信号是终端根据注销进程发出警告的方式。
Here's a shot in the dark: you could try using php's OS execution functions with
&
.Additionally, if that doesn't work, you can try
Edit: nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.
pcntl_fork()
会做与您最终想要完成的事情类似的事情吗? http://www.php.net/manual/en/function。 pcntl-fork.phpWould
pcntl_fork()
do something similar to what you're ultimately trying to accomplish? http://www.php.net/manual/en/function.pcntl-fork.php