Connecting Tech Pros Worldwide Forums | Help | Site Map

Possible to run a PHP function in background?

laredotornado@zipmail.com
Guest
 
Posts: n/a
#1: Jul 29 '08
Hi,

I'm using PHP 5. I have a function that takes a little while to run,
and I don't want the user to have to wait while it does. Is there a
way to run this function in the background?

Thanks for your help, - Dave

Jerry Stuckle
Guest
 
Posts: n/a
#2: Jul 29 '08

re: Possible to run a PHP function in background?


laredotornado@zipmail.com wrote:
Quote:
Hi,
>
I'm using PHP 5. I have a function that takes a little while to run,
and I don't want the user to have to wait while it does. Is there a
way to run this function in the background?
>
Thanks for your help, - Dave
>
Within the web server environment, not really. But you can start a CLI
job to do the work if your hosting company allows them. Or you can
queue up the request (i.e. in a database) and have a cron job run
regularly to handle the queue.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

EGreg
Guest
 
Posts: n/a
#3: Jul 29 '08

re: Possible to run a PHP function in background?


On Jul 28, 10:56 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Quote:
Hi,
>
I'm using PHP 5. I have a function that takes a little while to run,
and I don't want the user to have to wait while it does. Is there a
way to run this function in the background?
>
Thanks for your help, - Dave
Well, there are many ways you can run a function asynchronously (i.e.
not wait until it returns). One way is to fork the process. Another
way is to call some asynchronous function exposed by a PHP extension,
but then how will you get its result? You might have to call the
function again from another script, to collect the return value. The
second time around, it should BLOCK on a semaphore or better yet, on a
message queue. Look those up.
DS
Guest
 
Posts: n/a
#4: Jul 29 '08

re: Possible to run a PHP function in background?


On 2008-07-28 19:56:49 -0700, "laredotornado@zipmail.com"
<laredotornado@zipmail.comsaid:
Quote:
Hi,
>
I'm using PHP 5. I have a function that takes a little while to run,
and I don't want the user to have to wait while it does. Is there a
way to run this function in the background?
>
Thanks for your help, - Dave
We had a similar problem, and our large job took so much time, we split
it up into 10 background processes which filtered URLs in a large
database -- each of the 10 processes were operating on 1/10th of the
records in the main table. So this is what we did.

Note sending output to '/dev/null' surpressed any output from the
process; results from the PHP script were written to another database
table.

The ampersand at the end & meant to run the process in the background
(as a lower priority than foreground processes.)

Of course this is an approach that can be used if you are using
colocation or a dedicated server; if you use shared hosting, your ISP
may limit the use of the PHP exec () command:

$pathToPHPbinary = '/u01/apps/php/5.1.4/bin/php';
$pathToScriptToSpawn = '/u01/apps/www/runTests/siteFilter.php';

$limit = 10;
for ($i=1; $i<=$limit; ++$i)
{
exec ("$pathToPHPbinary $pathToScriptToSpawn $i $limit >/dev/null &");
}


Hans-Werner Hilse
Guest
 
Posts: n/a
#5: Jul 29 '08

re: Possible to run a PHP function in background?


Hi,

DS <ds@noSpam.comwrote:
Quote:
We had a similar problem, and our large job took so much time, we
split it up into 10 background processes which filtered URLs in a
large database -- each of the 10 processes were operating on 1/10th
of the records in the main table. So this is what we did.
Long running job's shouldn't be run by a webserver SAPI PHP. One really
should use CLI for that. It really messes up the webserver's resource
management otherwise and there is no good reason. Oh, looking further
down, you seem to do that.

Running parallel processes might make sense if the database doesn't
utilize available processor cores by itself in a senseful way.
Quote:
The ampersand at the end & meant to run the process in the background
(as a lower priority than foreground processes.)
No, it should have the same priority. You'd need to »nice« the process
to give a different priority. It is in the »background«, however. But
it'll most certainly die if the shell dies... Use »nohup« to prevent
that.


Of course, in such scenarios, extra care is needed for proper locking,
at least, if data is modified.


-hwh
Olaf Schinkel
Guest
 
Posts: n/a
#6: Jul 29 '08

re: Possible to run a PHP function in background?


laredotornado@zipmail.com schrieb:
Quote:
Hi,
>
I'm using PHP 5. I have a function that takes a little while to run,
and I don't want the user to have to wait while it does. Is there a
way to run this function in the background?
>
Thanks for your help, - Dave
You can use AJAX.

Display the main-Screen, send the 10 request via AJAX to the server and
the user can go ahead.
Hans-Werner Hilse
Guest
 
Posts: n/a
#7: Jul 29 '08

re: Possible to run a PHP function in background?


Hi,

Olaf Schinkel wrote:
Quote:
laredotornado@zipmail.com schrieb:
Quote:
Hi,

I'm using PHP 5. I have a function that takes a little while to
run, and I don't want the user to have to wait while it does. Is
there a way to run this function in the background?

Thanks for your help, - Dave
You can use AJAX.

Display the main-Screen, send the 10 request via AJAX to the server
and the user can go ahead.
Those 10 connections would be aborted/cut if the user actually does go
ahead. The Javascript context would be ripped out. Except, of course,
if you never go to a new page and »going ahead« is rather meant as
using AJAX for that, too.

But it is really a design problem to run long-running (»batch«-style)
tasks by utilizing a web server for process control.


-hwh
Olaf Schinkel
Guest
 
Posts: n/a
#8: Jul 29 '08

re: Possible to run a PHP function in background?


Hans-Werner Hilse schrieb:
Quote:
Hi,
>
Olaf Schinkel wrote:
>
Quote:
>laredotornado@zipmail.com schrieb:
Quote:
>>Hi,
>>>
>>I'm using PHP 5. I have a function that takes a little while to
>>run, and I don't want the user to have to wait while it does. Is
>>there a way to run this function in the background?
>>>
>>Thanks for your help, - Dave
>You can use AJAX.
>>
>Display the main-Screen, send the 10 request via AJAX to the server
>and the user can go ahead.
>
Those 10 connections would be aborted/cut if the user actually does go
ahead. The Javascript context would be ripped out. Except, of course,
if you never go to a new page and »going ahead« is rather meant as
using AJAX for that, too.
>
But it is really a design problem to run long-running (»batch«-style)
tasks by utilizing a web server for process control.
>
Yes. It is a design problem.
And....PHP is slow.
I write some programs in C++, when i have such problems.

Erwin Moller
Guest
 
Posts: n/a
#9: Jul 29 '08

re: Possible to run a PHP function in background?


Olaf Schinkel schreef:
Quote:
laredotornado@zipmail.com schrieb:
Quote:
>Hi,
>>
>I'm using PHP 5. I have a function that takes a little while to run,
>and I don't want the user to have to wait while it does. Is there a
>way to run this function in the background?
>>
>Thanks for your help, - Dave
You can use AJAX.
>
Display the main-Screen, send the 10 request via AJAX to the server and
the user can go ahead.
Hi Olaf,

Using Ajax for such a task is Bad Idea I think.
Using Ajax to call 10 scripts adds the client (=browser) into your chain
of logic, which makes your app unreliable.
What is user goes away just before your Ajax is executed?
What happens if JavaScript is not on?

If those 10 processes are important for the app, you want to make sure
they get executed. Putting the browser of the client in command of that
is dangerous and unreliable. It is wiser to let the script take care of it.

Regards,
Erwin Moller
binaryjesus
Guest
 
Posts: n/a
#10: Jul 29 '08

re: Possible to run a PHP function in background?


On Jul 29, 7:56 am, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Quote:
Hi,
>
I'm using PHP 5. I have a function that takes a little while to run,
and I don't want the user to have to wait while it does. Is there a
way to run this function in the background?
>
Thanks for your help, - Dave
i am taking your "little-while" to be less than 5 mins and also
guessing that ur on a linux shared host.

what u can do is exec("wget -b ". "http://site/put_heavy_func_here.php?
all_the_data" );
-b will put webget in the background amd exec would return
immediately.

this is almost like ajax but much much better as its bound to be
called by the server it self which is what u want. nad u can get the
result through akax if the need be.


in case ur denied access to wget (didnt mention its a linux
downloader) or on a windows serer than u can use
$dump = get_file_content("http://link.here") curl is always there as a
backup
hope this helps

binaryjesus
laredotornado@zipmail.com
Guest
 
Posts: n/a
#11: Jul 29 '08

re: Possible to run a PHP function in background?


On Jul 29, 8:22*am, binaryjesus <coolman.gu...@gmail.comwrote:
Quote:
On Jul 29, 7:56 am, "laredotorn...@zipmail.com"
>
<laredotorn...@zipmail.comwrote:
Quote:
Hi,
>
Quote:
I'm using PHP 5. *I have a function that takes a little while to run,
and I don't want the user to have to wait while it does. *Is there a
way to run this function in the background?
>
Quote:
Thanks for your help, - Dave
>
i am taking your "little-while" to be less than 5 mins and also
guessing that ur on a linux shared host.
>
what u can do is exec("wget -b ". "http://site/put_heavy_func_here.php?
all_the_data" );
-b will put webget in the background amd exec would return
immediately.
>
this is almost like ajax but much much better as its bound to be
called by the server it self which is what u want. nad u can get the
result through akax if the need be.
>
in case ur denied access to wget (didnt mention its a linux
downloader) or on a windows serer than u can use
$dump = get_file_content("http://link.here") curl is always there as a
backup
hope this helps
>
binaryjesus
Thanks all for your answers. binaryjesus has the solution that is
easiest for me to implement. Also, great handle! - Dave
Closed Thread