Is process forking in PHP / Apache a good idea?-ThrowExceptions

Exception or error:

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?

How to solve:

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.


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.


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.

Leave a Reply

Your email address will not be published. Required fields are marked *