Is process forking in PHP / Apache a good idea?

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?


父进程将结束,用户的页面将全部加载,子进程将继续,并且使用不会反馈子进程是否成功完成。


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.


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.

链接地址: http://www.djcxy.com/p/42146.html

上一篇: 是否可以使用Netbeans来处理VB6项目?

下一篇: PHP / Apache中的进程分叉是一个好主意吗?