473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Possible to run a PHP function in background?

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
Jul 29 '08 #1
10 13541
la***********@zipmail.com wrote:
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.
js*******@attglobal.net
==================

Jul 29 '08 #2
On Jul 28, 10:56 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
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.
Jul 29 '08 #3
DS
On 2008-07-28 19:56:49 -0700, "la***********@zipmail.com"
<la***********@zipmail.comsaid:
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 &");
}
Jul 29 '08 #4
Hi,

DS <ds@noSpam.comwrote:
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.
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
Jul 29 '08 #5
la***********@zipmail.com schrieb:
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.
Jul 29 '08 #6
Hi,

Olaf Schinkel wrote:
la***********@zipmail.com schrieb:
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
Jul 29 '08 #7
Hans-Werner Hilse schrieb:
Hi,

Olaf Schinkel wrote:
>la***********@zipmail.com schrieb:
>>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.

Jul 29 '08 #8
Olaf Schinkel schreef:
la***********@zipmail.com schrieb:
>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
Jul 29 '08 #9
On Jul 29, 7:56 am, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
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
Jul 29 '08 #10
On Jul 29, 8:22*am, binaryjesus <coolman.gu...@gmail.comwrote:
On Jul 29, 7:56 am, "laredotorn...@zipmail.com"

<laredotorn...@zipmail.comwrote:
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
Thanks all for your answers. binaryjesus has the solution that is
easiest for me to implement. Also, great handle! - Dave
Jul 29 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fabri | last post by:
<script> /* I would like to write a function that, onclick post ONLY input A, not B. Is it possible? */ function f(){ document.F.B /* destroy */ document.F.submit();
14
by: Mike Labosh | last post by:
How do you define whether the return value of a function is ByRef or ByVal? I have a utility class that cleans imported records by doing *really heavy* string manipulation in lots of different...
6
by: Francois Bonzon | last post by:
Any idea how I can launch a background task from a PHP script? For example, when a user posts on my message board, it may fire many e-mail notifications to other users, and other tasks. I want...
17
by: binjobster | last post by:
Hello everyone, I'm updating some documents that describes so many C/C++ functions/method(about 2500). But I'm not familiar with these codes. So I want to find such a utility can generate...
5
by: dj | last post by:
I somehow understand why member function templates cannot be virtual, but my question is how I could achieve something similar to this: class A { public: template<typename Tvirtual f() = 0; }...
4
by: Chris | last post by:
Hello, I have two div's, div1 is a lot bigger and is the parent of div2, and div1 also has a background image. What I would like to do, is set a background color for the smaller nested div2,...
28
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a...
1
by: holdingbe | last post by:
Hi all, In Stored function, is any possible to give more record as one input variables. for example, sample program: create or replace function sample_test (in_variable in varchar2)
5
johny10151981
by: johny10151981 | last post by:
spin_lock(&net_family_lock); if (net_families) err = -EEXIST; else { net_families = ops; err = 0; } spin_unlock(&net_family_lock); Please look at the above piece of code (got from...
1
by: kalees waran | last post by:
is it possible function available to use multiple headers in a page
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.