1. Go to this page and download the library: Download seankndy/daemon 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/ */
seankndy / daemon example snippets
use SeanKndy\Daemon\Daemon;
use SeanKndy\Daemon\Processes\Event as ProcessEvent;
$maxProcesses = 50;
$quietTime = 1000000;
$childTimeout = 30;
$daemon = new Daemon($maxProcesses, $quietTime, $childTimeout);
// $producer can be a callable or an object that implements SeanKndy\Daemon\Tasks\Producer
// it should produce work to do (which is also a callable or an object implementing SeanKndy\Daemon\Tasks\Task)
//
// if multiple producers are added, they will be round-robined
$number = 0;
$producer = function() use (&$number) {
if ($number >= 10) return null;
$number++;
// this is the "task" or the code to run within the forked child
return function() use ($number) {
echo "hello from child $number, my pid is " . getmypid() . "\n";
return 0; // child exit value
};
};
$daemon->addProducer($producer);
// optional, signal catching
$daemon->addSignal(SIGTERM, function ($signo) {
// SIGTERM caught, handle it here
});
// optional, example event listeners
$daemon->addListener(ProcessEvent::START, function ($event) {
// process $event->getProcess() started
});
$daemon->addListener(ProcessEvent::EXIT, function ($event) {
// process $event->getProcess() exited
});
$daemon->setDaemonize(false); // don't fork to background
$daemon->start();
use SeanKndy\Daemon\Tasks\Task;
class MyTask implements Task
{
private $ipc;
/**
* Initialize task (main thread)
*
* @return void
*/
public function init() : void
{
$this->ipc = new \SeanKndy\Daemon\IPC\Socket();
}
/**
* Do the work (child thread)
*
* @return int
*/
public function run() : int
{
// do something useful in child and generate $interestingData
$this->ipc->send($interestingData);
$this->ipc->close();
}
/**
* Cleanup (main thread)
*
* @var int $status Exit status of child thread
* @return void
*/
public function finish(int $status) : void
{
if ($this->ipc->hasMessage()) {
$interestingData = $this->ipc->receive();
// $interestingData now in parent process
}
$this->ipc->close();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.