473,465 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
5 4260
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hopeless A | last post by:
We are attempting to fork a php script into a background php script and have the first scrip continue to completion. Is this even possible in the windows version of php? We have used the many code...
0
by: Eric S. Johansson | last post by:
I was working on a filter for postfix and instead of using the "fork a new python process on every message" route, I decided to use the SMTP interface instead and try forking after having started...
1
by: SRam | last post by:
How to create a threaded process in Perl. I need explanation for this code from Advanced Perl Programming.... # Forking server use IO::Socket; $SIG{CHLD} = sub {wait ()}; $main_sock = new...
0
by: SRam | last post by:
I a writing Forking Server for a Small application.. This is code for multiplexing my $listen = IO::Socket::INET->new(Proto => 'tcp',LocalPort => 2323, Listen => 1, Reuse => 1) or die $!; ...
3
by: felixfix | last post by:
Hi all, I am just wondering if something is wrong with my program. What it bascially does is to output a fibonacci sequence base on the command-line output. If I give a 5, it will generate the...
3
by: czajnik | last post by:
Hi! I'm quite new to Python development. Can someone advise me a framework useful for building (pre-)forking or threaded TCP servers, other than SocketServer ? I've seen source code of a few...
2
by: dakman | last post by:
I know under mac/*nix it's possible to fork the proccess to the background and exit the parent process. I have used this on a couple of my projects, but right now I am working on a project that...
10
by: qwertycat | last post by:
I'm new to multi-process programming, should one avoid forking children from children of a parent? I'd like to spawn 10 children from the parent and each of those children spawns another 5...
3
by: Scottman | last post by:
I am writing a server daemon that forks a child for every incoming request. The child process terminates when the client closes the connection. My problem is that I am unsure how to prevent the...
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
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
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...
1
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
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...
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.