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

I am writing a simple PHP application that sometimes has to do a fairly intense set of MySQL updates. I don’t really want this to cause a delay for the user, so I'm curious about using pcntl_fork ().

I'm not sure how this works: will the child process continue after the parent process completes? Will the parent process end and the user page will be fully loaded before the child process is complete?

In other words, is this a safe way to get a PHP script (running under Apache) to perform some time-consuming updates without delaying the user, or just asking my users to put up with the delay?

+7
php process fork
source share
4 answers

The parent process will complete, the user page will load completely, the child process will continue, and use will not have feedback on whether the child process completed successfully.

+3
source share

Someone may tell you in detail what happens when you call it under apache, but most likely you will get answers that are not always true depending on which versions and combinations of apache and php you use. You must use ajax and have two requests. Repeat once with a page that says what you are doing, and then polling ajax calls a second status request and where you are actually doing the work.

+1
source share

If PHP is running under Apache, since the formatting of the mod_php module will not work at all, you will get a warning that the * pcntl_fork () * function is undefined. In this case, a good solution is to use exec () instead to run a separate php job using the command line.

0
source share

I think this is a bad idea. I made similar stuff, and apache redirects the parent's output to its child. This is your browser showing information from one of the child processes.
Click this for more information.
Hope this helps you.

0
source share

All Articles