Answer the question
In order to leave comments, you need to log in
Deferred code execution in php
Greetings!
I faced such a question - how to implement the following:
The user enters the a.php page, redirects him to b.php and at the same time c.php is called and executed C.php
is a resource-intensive script that I would like to run in the background, in while the user went about his business on b.php
Answer the question
In order to leave comments, you need to log in
You can do this:
exec("/usr/bin/php /var/www/c.php > /dev/null &");
Then exec will release the console and the b.php script will continue to run and c.php will run in the background.
ZendX_Console_Process_Unix . True, it only works on *niks and requires the php-extension shmop, pcntl and posix. But it is easy to use, like boots.
the user went to the page and the ajax post-request was launched, in my opinion the simplest and most logical solution.
In b.php on the server side, send a POST request (using sockets) to c.php and close the connection without waiting for a response. But for this, you will need to enable ignore_user_abort in c.php.
c.php is loaded by ajax or iframe.
in the settimelimit(0) script to remove the time limit,
if the task can be divided into a "portion", then it's good. then execute in small portions with the same Ajax, then you can even show the loading indicator.
We went to a.php, created a task.txt file with a line for parameters (if necessary)
Then, by cron, we run the script-checker of the task.txt file every minute, which executes the necessary command
exec("/usr/bin/ php /var/www/c.php > /dev/null");
Check out Gearman ( gearman.org/ ). It suits just such purposes and there is integration with php. Review article — highload.com.ua/index.php/2010/07/09/gearman-and-php-asynchronous-tasks/
If you have your own server or VPS, use beanstalkd - this is a queue server. Written in Syakh, simple, fast, quite a lot of features like a task postponed for a given time. The interface to the puff is client (not an extension), although for me you can write your own on sockets - the protocol is very simple there
Search bots and ordinary visitors do not spare your web server much. An optimized per-minute cron request will get lost in their background. If there is no mandatory connection to the database for any client request + the flag of the presence of tasks in the queue hangs in the cache (memcached, file, etc.), then this load will not be critical.
fpm - "fastcgi_finish_request() - special function to finish request and flush all data while continuing to do something time-consuming"
the user went to the page and the ajax post-request was launched, in my opinion the simplest and most logical solution.
To execute a script in parallel in the background, there is such a thing as threads.
In syphony there is a suitable implementation - Process It is very easy
to integrate into the project .
Start a separate process in b.php when you need it, and if suddenly c.php crashes b will continue to run
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question