C
C
Chvanikoff2010-10-08 10:18:35
PHP
Chvanikoff, 2010-10-08 10:18:35

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

14 answer(s)
E
Effrenus, 2010-10-08
@Effrenus

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.

W
witbier, 2010-10-08
@witbier

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.

M
MOst_53, 2010-10-10
@MOst_53

the user went to the page and the ajax post-request was launched, in my opinion the simplest and most logical solution.

I
iStyx, 2010-10-08
@iStyx

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.

O
ojiga, 2010-10-08
@ojiga

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.

E
Eugene, 2010-10-08
@Nc_Soft

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");

S
Shedar, 2010-10-08
@Shedar

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/

W
witbier, 2010-10-08
@witbier

well, since we are talking about crowns, then man at

B
BasilioCat, 2010-10-08
@BasilioCat

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

D
digi, 2010-10-09
@digi

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.

A
Arekus, 2010-10-10
@Arekus

fpm - "fastcgi_finish_request() - special function to finish request and flush all data while continuing to do something time-consuming"

M
MOst_53, 2010-10-10
@MOst_53

the user went to the page and the ajax post-request was launched, in my opinion the simplest and most logical solution.

A
AvrGavr, 2013-05-21
@AvrGavr

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

L
lepjep, 2016-10-12
@lepjep

see here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question