php/forking...
| From: | bruce | Date: | Sat, 15 Nov 2014 17:14:57 +0000 |
| Subject: | php/forking... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-324045@lists.php.net to get a copy of this message | ||
what's the issue here??
Got a quick test of the php pnctl funcs.
Basically, simply trying to run a number of child functions in
parallel. I was under the impression that forking off the processes
would essentially run them in parallel/simultaneously.
I have a parentApp, as well as a child.php that I include.
When I run the test, the fork stuff runs.. but it runs sequentially.
So I'm not sure what I've missed..
My expectation is that the test/parent would quickly fire off all
instances of the forked process, and in the procTBL, I'd be able to
see a number of the children running at the same time..
I can more or less get this behaviour, if I replace the forked section
with a "system(foo.php &)
The test is:
#!/usr/bin/php
<?php
/*
* FileName: testfork.php
*
* quick test of the php fork
*
*
*/
$maxCount=1000; //restart/recreate the GearmanWorker very
masterCount iterations
$mCount=0;
$rStart=true;
$pid=array();
$i=0;
print "Waiting for a job...\n";
#while($worker->work());
//$worker->work();
/*
-start the top level check to see if the
queue/proc is running.
-the logic being, if the queue isn't running, the
stats_check will not have the "." so the loop does
a sleep/wait and tries again.. this continues
until the queue runs..
-when the queue is running.. the loop then performs
as normal.. however in the normal process, the
logic now also checks to see if the JobCronProcess
is requesting the job/queues, or if the queue isn't
running..
-if the JobCronProcess is requesting, the
logic then sets the pid in the fooProcessQueuePID.dat
*/
$top=true;
$aa=0;
while($top)
{
$p1=posix_getpid();
$p1=trim($p1);
//$pidcount="pgrep -f 'testfork' | wc -l";
$pidcount='ps aux | grep "testfork" | grep "Child" | grep
-v "grep" | wc -l';
//$count=
$pidcount;
$count=1;
if((int)trim($count)<$maxCount+1)
{
print "hh \n";
print "in queue ".$p1." --firing off work \n";
$data="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
fork1($data,$aa);
$aa=$aa+1;
//sleep(1);
if($aa==5)
exit();
}
else
{
$top=""; //x num of children happened
//- force recreation of the worker
//sleep(5);//wait a bit
}
}
exit();
/*
setup the function to process the data on the
fork
process the data, update the required tbl(s)
update the required/associated tmp db/tbl/queues
as well as the associated parse/crawl tbls
create/build the required FetchRequestQueue
packet/data for the next level of the
crawl if required
*/
function fork1($data,$i) {
print "ed \n";
global $pid;
if(strlen($data)>0)
{
$pid[$i] = pcntl_fork();
if($pid[$i] == -1) {
die("could not fork");
} else if($pid[$i]) {
pcntl_wait($status);
} else {
print "fork \n";
func1($data);
exit();
}
}
}
function func1($data)
{
$child="forkfunc1.php";
include($child);
}
?>
<?php
/*
* FileName: forkfunc1.php
* forkfunc1.php
*
* test include file/function for the testfork
*
*/
$includeLib=getenv('yolo_masterYoloMasterApps_IncludeDirPhp');
include($includeLib.'/DB_conf_inc.php');
/*
setup db access
setup to use tcp - instead of localhost/sockets...
*/
//$pdo_def="mysql:host=".$dbhost.";dbname=".$dbname;
$pdo_def="mysql:host=".$dbhost.";port=".$dbport.";dbname=".$dbname;
$pdo = new PDO($pdo_def,$dbuser,$dbpass);
/*
//--simulate time in app..
sleep (20); //20 secs..
print "here func";
exit();
?>