Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is process forking in PHP / Apache a good idea?

Tags:

php

fork

process

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?

like image 272
Ben Avatar asked Nov 25 '08 13:11

Ben


2 Answers

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.

like image 58
configurator Avatar answered Nov 10 '22 13:11

configurator


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.

like image 22
carson Avatar answered Nov 10 '22 15:11

carson