473,624 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13589
la***********@z ipmail.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*******@attgl obal.net
=============== ===

Jul 29 '08 #2
On Jul 28, 10:56 pm, "laredotorn...@ zipmail.com"
<laredotorn...@ zipmail.comwrot e:
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:

$pathToPHPbinar y = '/u01/apps/php/5.1.4/bin/php';
$pathToScriptTo Spawn = '/u01/apps/www/runTests/siteFilter.php' ;

$limit = 10;
for ($i=1; $i<=$limit; ++$i)
{
exec ("$pathToPHPbin ary $pathToScriptTo Spawn $i $limit >/dev/null &");
}
Jul 29 '08 #4
Hi,

DS <ds@noSpam.comw rote:
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***********@z ipmail.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***********@z ipmail.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***********@z ipmail.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***********@z ipmail.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.comwrot e:
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_conten t("http://link.here") curl is always there as a
backup
hope this helps

binaryjesus
Jul 29 '08 #10

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

Similar topics

3
267
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
2557
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 methods, and it operates on DataTables in excess of 100,000 rows of anywhere between 4 and 30 columns. I'd like to get the functions to return ByRef for greater speed and memory efficiency, but I can't see how to do that. --
6
9646
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 the posting confirmation page to end displaying quickly, without waiting for those notifications or other tasks to complete. So basically what I want is to launch 'background.php' from 'main.php', with the user seeing only the output of - and...
17
2024
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 returning value list from the codes. Is it possible? Or is it hard to write one? Thanks.
5
2958
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; } class B : public A { public:
4
3928
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, but at the same time it is transparent and the background image of div1 shines through. I hope my explanation is a bit clear. Is this kind of effect at all possible, without using a background image for
28
16443
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 subset of C/C++, thus will have the same syntax as C/C++. Somehow the interpreted code must be able to store generic function pointers because there is no way for the interpreter to know every possible function signature in advance. I was...
1
1341
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
2212
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 net/socket.c). Its about spin_lock function. I tried to find the explanation. But its not that easy.
1
1735
by: kalees waran | last post by:
is it possible function available to use multiple headers in a page
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.