clamp
clamp

Reputation: 34016

PHP fork excessive task

When a user registers on my php driven site, the register script has to perform a couple of tasks some of take longer because they have to contact 3rd party servers.

how can i fork these tasks away from the main php script so that the user see the result right away?

Upvotes: 0

Views: 442

Answers (4)

Neil Richins
Neil Richins

Reputation: 91

If the 3rd party requests are not needed to display the page, you can always fork a PHP script in the background by doing a

shell_exec('echo /usr/bin/php -q myLongScript.php | at now');

Instead of forking a new instance to handle 3rd party registrations every time a new user registers, you might also consider storing the information in a database table and process the 3rd party registrations at scheduled intervals through a PHP cronjob script.

Run PHP through Cron job

Upvotes: 0

symcbean
symcbean

Reputation: 48357

Assuming that you are happy to send a response to the user independently of the 3rd party requests....

If you ignore_user_abort() and send a header("Connection: close"); and flush the output buffer, then you can simply call the remote sites via a register_shutdown_function(). If there is more than one remote request to be processed then use the curl_multi functions to run them in parallel.

Upvotes: 0

sockeqwe
sockeqwe

Reputation: 15929

Not sure what you want to do ...

you can execute a extern php script with exec('php myScript.php');

But than the user don't get the result of this script, because the user only gets the result of the main php script ...

If you want to get the user the result of all executed scripts than you should use ajax calls

Upvotes: 0

Tim S.
Tim S.

Reputation: 13843

You can create a child process, or just create a queue in your database or a small file and run a cronjob. You can also create a child process by running exec().

exec('php /path/to/script.php');

Upvotes: 2

Related Questions