2

I'm making a PHP application that spawns child processes with parameters detailing what work they are supposed to do. More specifically, the child processes will process rows from a large MySQL database, and the parent application will count the rows and spawn ~50 processes with a span of rows to process.

I need a way for the parent process to know if the child processes are done. I've made this application in Linux before, using a MySQL table for the child processes to check in to. I want to do the process management independent of MySQL this time (And now I'm in Windows).

Is there a way for a PHP parent process to acquire a "handle" to a child process at it's creation and watching it for activity?

I hope I'm clear on the gist of what I'm trying to do. Any answers and advice would be appreciated.

Except for advice to use another programming language - there are lots of PHP libraries I'm using for the processing as well as custom classes and functions

Hubro
  • 56,214
  • 69
  • 228
  • 381
  • Maybe interesting http://php.net/pcntl-wait In fact a children should not need to know, which PID its parent has. Instead the parent should have an eye on its children :) – KingCrunch May 17 '11 at 22:34
  • http://php.net/manual/en/function.getmypid.php `getmypid()`. Not sure how to pass this to children - in fact, not sure how to fork, so I hope you get an answer to this one – Adam May 17 '11 at 22:35
  • http://stackoverflow.com/questions/601670/whats-the-best-way-to-fork-thread-in-php-on-win also looks interesting – Adam May 17 '11 at 22:36

3 Answers3

1

I once built a small sample that launches copies of itself for some performance test. The main script used proc_open() to call the child processes. That way, you can redirect the in- and output of your script to the main script and exchange commands / data. I no longer have the source of that script but this is a rough outline:

$fork = array();
$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
);
for ($i = 0; $i < 50; $i++){
    $fork[$i] = array('process' => NULL, 'pipes' => array());
    $fork[$i]['process'] = proc_open('php script.php', $descriptorspec, $fork[$i]['pipes']);
}
# Loop thru $fork, check feof of the pipes
# write commands, read data
# if all pipes return feof, proc_close($fork[$i]['process'] will return the returncode

I hope something of this is useful to you ; )

Stephan B
  • 3,671
  • 20
  • 33
  • Stephan B: If I get your solution working, I'll mark this answer as correct. Why don't you help me out? :-) http://stackoverflow.com/q/6045884/388916 – Hubro May 18 '11 at 14:33
0

You could make it way more easly to manage.

Run these scripts with a param so they will do a query with a LIMIT param and will elaborate whatever you need on a "slice" of the total query result

dynamic
  • 46,985
  • 55
  • 154
  • 231
0

You can't fork in Windows, this is the simple truth. Linux forks, Windows threads.

But i have found this (threading with php in windows - has some resources):

What's the best way to fork/thread in PHP on Win?

Community
  • 1
  • 1
beerwin
  • 9,813
  • 6
  • 42
  • 57
  • I really don't care much about the terminology. The outcome is the same; A process creates other processes. Thanks for the link – Hubro May 18 '11 at 06:01