By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Forking a PHP process into the background in a web server environment

P: n/a
JDS
Hi. I have installed the PCNTL functions[1] on my PHP enabled webserver
in the interest of being able to fork a process into the background so I
can do some heavy processing but return control of the browser back to the
user rather quickly.

However, after mucking about with PCNTL for a while and not getting the
results I expected, I finally stumbled across this line in the opening
parragraph on PCNTL functions on PHP.net:

"Process Control should not be enabled within a webserver environment and
unexpected results may happen if any Process Control functions are used
within a webserver environment."

Seems like they should bold and highlight in red that line, but I digress.

In any case, can anyone suggest a way to fork a process off to unload a
large amount of processing and return control of the web browser to the
user? All the examples and whatnot I have found assume a CLI version of
PHP. I want to do this on the web server!

Thanks!!

[1]I am running Apache2/PHP4.3 on RHEL 4. I downloaded the RPM for PCNTL
at http://phprpms.sourceforge.net.

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Mar 21 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
JDS wrote:
In any case, can anyone suggest a way to fork a process off to unload a
large amount of processing and return control of the web browser to the
user? All the examples and whatnot I have found assume a CLI version of
PHP. I want to do this on the web server!


Run things with help of cron.
//Aho
Mar 21 '06 #2

P: n/a
ED

"JDS" <je*****@example.invalid> wrote in message
news:pa****************************@example.invali d...
Hi. I have installed the PCNTL functions[1] on my PHP enabled webserver
in the interest of being able to fork a process into the background so I
can do some heavy processing but return control of the browser back to the
user rather quickly.

However, after mucking about with PCNTL for a while and not getting the
results I expected, I finally stumbled across this line in the opening
parragraph on PCNTL functions on PHP.net:

"Process Control should not be enabled within a webserver environment and
unexpected results may happen if any Process Control functions are used
within a webserver environment."

Seems like they should bold and highlight in red that line, but I digress.

In any case, can anyone suggest a way to fork a process off to unload a
large amount of processing and return control of the web browser to the
user? All the examples and whatnot I have found assume a CLI version of
PHP. I want to do this on the web server!

Thanks!!

[1]I am running Apache2/PHP4.3 on RHEL 4. I downloaded the RPM for PCNTL
at http://phprpms.sourceforge.net.

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/


Myabe a bit of a hack but one way is to make a http connection to the script
then immediately close it:

function forkHTTP($host, $script) {
$conn = @fsockopen($host, 80);
if ($conn) {
$cmd = "GET $script HTTP/1.1\r\n";
$cmd .= "Host: $host\r\n\r\n";
@fwrite($conn, $cmd);
@fclose($conn);
return true;
}
return false;
}

Just call the function to execute the process.

if (forkHTTP('www.mydomain.com', '/scripts/heavyprocess.php')) {
// OK - process called
} else {
//erm
}

HTH ED
Mar 21 '06 #3

P: n/a
JDS
On Tue, 21 Mar 2006 19:42:58 +0100, J.O. Aho wrote:
Run things with help of cron.


No, you see, I want to be able to fork a process from a user-initiated
session from a web browser. Specifically, this is for a mass-emailer.

In any case, I figured out a way to do it. I didn't want to install PHP
CLI so I used Perl instead. Now I have to debug the Perl script for
securiy flaws, stability, and reduced resource use, but the durn thing
does work as intended. Quickly returns control of the web browser to the
user, even for very very long email lists. Continues to process said
mailing list in the background.

General description:

(1) Create a simple mailing script that can run from the command line and
will accept CLI arguments. Script can really be in any language, PHP,
Perl, C, Ruby, Python, whatever -- it is a separate entity from the PHP
script that interacts with the browser.

(2) Execute the CLI script using PHP's exec() function. Include ampersand
at end and direct STDOUT to /dev/null

This won't work on Windows, I'm durn sure of that. But Windows sucks
ass nuggets anyways, so who would be using Windows as a PHP-enabled web
server?

In any case, I got the inspiration from this page:

http://www.phpfreaks.com/tutorials/71/0.php

It should be noted that I do not use the PCNTL functions at all for this
task. (Side note: So I'm uninstalling them.)

And finally, I will post the entire exercise to my website if anyone
requests it. Always glad to help if I can.

word to your momma. Later...

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Mar 21 '06 #4

P: n/a
JDS wrote:
On Tue, 21 Mar 2006 19:42:58 +0100, J.O. Aho wrote:
Run things with help of cron.


No, you see, I want to be able to fork a process from a user-initiated
session from a web browser. Specifically, this is for a mass-emailer.


sending out bulk e-mails aren't that time depending, you can still let cron do
the mailing, you use only the php as a front end to design the mail to send.
It don't matter if it takes another 59 minutes before it really is sent (if
you have set the cron to check if there is any outgoing once a hour).
//Aho
Mar 22 '06 #5

P: n/a
JDS
On Wed, 22 Mar 2006 16:28:33 +0100, J.O. Aho wrote:
sending out bulk e-mails aren't that time depending, you can still let cron do
the mailing, you use only the php as a front end to design the mail to send.
It don't matter if it takes another 59 minutes before it really is sent (if
you have set the cron to check if there is any outgoing once a hour).
//Aho


A good idea that I hadn't considered. Thanks! I suppose this method will
even allow you to cancel a message if it has not been sent yet.

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Mar 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.