1. Go to this page and download the library: Download sue/child-process library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sue / child-process example snippets
use Sue\ChildProcess\Process;
use function Sue\EventLoop\loop;
$process = new Process('php demo.php');
$process->attach();
loop()->run();
use Sue\ChildProcess\Process;
use Sue\ChildProcess\Exceptions\TimeoutException;
use function Sue\EventLoop\loop;
$process = new Process('php demo.php');
$process->setMaxRunningSeconds(15);
$process->attach()
->then(
function () {
echo "process executed successfully\n";
},
function (TimeoutException $e) {
echo "超时: " . $e;
}
);
loop()->run();
use Sue\ChildProcess\PersistentProcess;
use Sue\ChildProcess\Exceptions\ProcessException;
use function Sue\EventLoop\loop;
$process = new PersistentProcess('php consumer.php');
$process->attach()
->otherwise(function (ProcessException $exception) {
echo "fail to start process\n";
});
loop()->run();
use Sue\ChildProcess\PersistentProcess;
use function Sue\EventLoop\loop;
$process = new PersistentProcess('php consumer.php');
$process->setMinUpTime(2.15)
->setMaxRetries(15)
->attach()
->then(
function () {
echo "process exited successfully\n";
},
function ($error) {
echo $error . "\n";
}
);
loop()->run();
$process = new PersistentProcess('php consumer.php');
$process->attach();
loop()->run();
$process = new Process('php work.php');
$process->attach();
$process->promise()->then(
function () {
echo "process end successfully\n"
},
function ($error) {
echo "process stop with error: " . $error . "\n";
}
);
use Sue\ChildProcess\PersistentProcess;
use function Sue\EventLoop\loop;
use function Sue\EventLoop\setInterval;
$time_start = time();
$process = new PersistentProcess('php consumer.php');
setInterval(1, function () use ($process, $time_start) {
if (time() - $time_start > 30) {
$process->terminate();
}
});
$process->attach();
loop()->run();
$process = new PersistentProcess('php consumer.php');
setInterval(1, function () use ($process, $time_start) {
if (time() - $time_start > 30) {
$process->terminate(null, new \RuntimeException('foo'));
}
});
$process->attach()->then(null, function (\RuntimeException $e) {
//error handle
});
//consumer.php
while (true) {
echo "hello world\n";
sleep(1);
}
//process.php
use Sue\ChildProcess\PersistentProcess;
use function Sue\EventLoop\loop;
$process = new PersistentProcess('php consumer.php');
$process->output(function ($chunk) {
echo $chunk;
});
$process->attach();
loop()->run();
/** expected output:
* hello world
* hello world
* hello world
* hello world
* ...
*/
//consumer.php
while (true) {
echo "hello world\n";
sleep(1);
}
throw new Exception('foo');
//process.php
use Sue\ChildProcess\Process;
use function Sue\EventLoop\loop;
$process = new Process('php consumer.php');
$process->errorOutput(function ($chunk) {
echo $chunk;
});
$process->attach();
loop()->run();
/** expected output:
* Fatal error: Uncaught Exception: foo in consumer.php:3
*/
use Sue\ChildProcess\PersistentProcess;
use function Sue\EventLoop\loop;
use function Sue\EventLoop\setInterval;
use function Sue\EventLoop\cancelTimer;
$process = new PersistentProcess('php consumer.php');
setInterval(5, function ($timer) use ($process) {
if ($process->isRunning()) {
echo "process looks good\n";
} else {
echo "process went wrong\n";
cancelTimer($timer);
}
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.