PHP / Apache 中的进程分叉是个好主意吗?
我正在用 PHP 编写一个简单的应用程序,它偶尔需要执行一组相当密集的 MySQL 更新。 我并不特别希望这会导致用户延迟,所以我想知道是否使用 pcntl_fork()。
我不确定这到底是如何工作的:父进程完成后子进程会继续运行吗? 父进程是否会结束,并且用户的页面加载会在子进程完成之前完全完成吗?
换句话说,这是让 PHP 脚本(在 Apache 下运行)执行一些耗时的更新而不延迟用户的安全方法,还是我应该要求我的用户忍受一些延迟?
I'm writing a simple application in PHP which needs to occasionally carry out a fairly intensive set of MySQL updates. I don't particularly want this to cause a delay for the user, so I'm wondering about using pcntl_fork().
I'm not sure how this really works though: will the child process continue running after the parent process finishes? Will the parent process end, and the user's page load fully complete before the child process completes?
In other words, is this a safe way to have a PHP script (running under Apache) do some time-consuming updates without delaying the user, or should I just ask my users to put up with some delay?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
父进程将结束,用户的页面将完全加载,子进程将继续,并且用户不会得到关于子进程是否成功完成的反馈。
The parent process will end, the user's page will load fully, the child process will continue, and the use will have no feedback as to whether or not the child process finished successfully.
有人可能会详细告诉您当您在 apache 下调用它时会发生什么,但您可能会得到并不总是正确的答案,具体取决于您使用的 apache 和 php 的版本和组合。 您应该使用 ajax 并有两个请求。 使用显示您正在做什么的页面响应一次,然后使用 ajax 调用轮询第二个请求,了解状态以及您实际执行工作的位置。
Someone out there can probably tell you in detail what happens when you call that under apache but the chances are you will get answers that aren't always true depending on what versions and combinations of apache and php you are using. You should use ajax and have two requests. Respond once with the page that says what you are doing and then with an ajax call poll a second request for the status and where you actually do the work.
如果 PHP 在 Apache 下运行,因为 mod_php 模块分叉根本无法工作,您将收到一条警告,指出函数 *pcntl_fork()* 未定义。 在这种情况下,一个好的解决方案是使用 exec() 来使用命令行运行单独的 php 作业。
If PHP runs under Apache as mod_php module forking will not work at all, you'll get a warning saying that function *pcntl_fork()* is undefined. In that case a good solution is to use exec() instead to run a separate php job using the command line.
我认为这是一个坏主意。 我已经做了类似的事情,并且 apache 将父级的输出重定向到其子级。 这是您的浏览器显示来自子进程之一的信息。
点击此了解更多信息
希望对您有帮助。
I think it is a bad idea. I have done the similar stuff, and the apache redirect the ouput of parent to its child. That is your browser shows the info from one of the child process.
Click this for more infomation
Hope it help you.