This is a library for PHPer to handle Child Process easy and simple. That's it.
- php5.3+
- pcntl
- posix
- pagon/eventemitter
add "pagon/childprocess": "*"
to you composer.json
:
composer.phar install
Current process handle
declare(ticks = 1) ;
$manager = new ChildProcess();
$manager->on('exit', function () use ($process) {
error_log('exit');
exit;
});
// To do something
Run the closure function in parallel child process space
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->parallel(function () {
sleep(10);
// to do something
});
To keep master running to handle the events, you can use
join
or usewhile(1)
for forever run
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->parallel(function () {
// to do something
sleep(10);
error_log('child execute');
}, false);
$child->on('exit', function ($status) {
error_log('child exit ' . $status);
});
// Will run but don't wait the child exit
$child->run()
while(1) { /*to do something */}
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->parallel(function () {
// to do something
sleep(10);
error_log('child execute');
}, false);
$child->on('exit', function ($status) {
error_log('child exit ' . $status);
});
// Will wait the child exit
$child->join();
Run the PHP file in parallel child process space
The Master:
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->fork(__DIR__ . '/worker.php', false);
$child->on('exit', function ($status) {
error_log('child exit ' . $status);
});
$child->join();
The Fork PHP file:
$process // The parent process
// Some thing to do in child process
Run the command in child process
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->spawn('/usr/sbin/netstat', false);
$child->on('stdout', function ($data) {
error_log('receive stdout data: ' . $data);
// to save data or process it
});
$child->on('stderr', function ($data) {
error_log('receive stderr data: ' . $data);
// to save data or process something
});
$child->join();
Message communicate between parent process and child process
declare(ticks = 1) ;
$manager = new ChildProcess();
$manager->listen();
$child = $manager->parallel(function (Process $process) {
$process->on('message', function ($msg) {
error_log('child revive message: ' . $msg);
});
$child->listen();
$process->send('hello master');
error_log('child execute');
}, false);
$child->on('message', function ($msg) {
error_log('parent receive message: ' . $msg);
});
$child->send('hi child');
$child->join();
Current setting supported:
array(
'cwd' => false, // Current working deirectory
'user' => false, // Startup user
'env' => array(), // Enviroments
'timeout' => 0, // Timeout
'init' => false, // Init callback
'callback' => false // Child startup callback
)
Some usage:
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->spawn('/usr/sbin/netstat', array(
'timeout' => 60 // Will wait 60 seconds
'callback' => function(){ error_log('netstat start'); }
), false);
$child->on('stdout', function ($data) {
echo $data;
// to save data or process it
});
$child->join();
tick
Every tick will trigger thislisten
Listen the messagesignal
When signal received, Allabort
Abort:SIGINT,SIGTERMfinish
Finish process (Normal exit)exit
When process is exit, includesabort
andfinish
events
listen
Process instance's manager listen the message queuerun
Process instance run in childinit
Process instance child process createdfork
Process instance forkabort
Process instance abort:SIGINT,SIGTERMfinish
Process instance finish process (Normal exit)exit
Process instance exit, includesabort
andfinish
events