PHP code example of jenner / simple_fork
1. Go to this page and download the library: Download jenner/simple_fork 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/ */
jenner / simple_fork example snippets
class TestRunnable implements \Jenner\SimpleFork\Runnable{
/**
* Entrance
* @return mixed
*/
public function run()
{
echo "I am a sub process" . PHP_EOL;
}
}
$process = new \Jenner\SimpleFork\Process(new TestRunnable());
$process->start();
$process->wait();
$process = new \Jenner\SimpleFork\Process(function(){
for($i=0; $i<3; $i++){
echo $i . PHP_EOL;
sleep(1);
}
});
$process->start();
$process->wait();
class Producer extends \Jenner\SimpleFork\Process{
public function run(){
$cache = new \Jenner\SimpleFork\Cache\SharedMemory();
//$cache = new \Jenner\SimpleFork\Cache\RedisCache();
for($i = 0; $i<10; $i++){
$cache->set($i, $i);
echo "set {$i} : {$i}" . PHH_EOL;
}
}
}
class Worker extends \Jenner\SimpleFork\Process{
public function run(){
sleep(5);
$cache = new \Jenner\SimpleFork\Cache\SharedMemory();
//$cache = new \Jenner\SimpleFork\Cache\RedisCache();
for($i=0; $i<10; $i++){
echo "get {$i} : " . $cache->get($i) . PHP_EOL;
}
}
}
$producer = new Producer();
$worker = new Worker();
$pool = new \Jenner\SimpleFork\Pool();
$pool->execute($producer);
$pool->execute($worker);
$pool->wait();
class Producer extends \Jenner\SimpleFork\Process
{
public function run()
{
$queue = new \Jenner\SimpleFork\Queue\SystemVMessageQueue();
//$queue = new \Jenner\SimpleFork\Queue\RedisQueue();
for ($i = 0; $i < 10; $i++) {
echo getmypid() . PHP_EOL;
$queue->put($i);
}
}
}
class Worker extends \Jenner\SimpleFork\Process
{
public function run()
{
sleep(5);
$queue = new \Jenner\SimpleFork\Queue\SystemVMessageQueue();
//$queue = new \Jenner\SimpleFork\Queue\RedisQueue();
for ($i = 0; $i < 10; $i++) {
$res = $queue->get();
echo getmypid() . ' = ' . $i . PHP_EOL;
var_dump($res);
}
}
}
$producer = new Producer();
$worker = new Worker();
$pool = new \Jenner\SimpleFork\Pool();
$pool->execute($producer);
$pool->execute($worker);
$pool->wait();
class TestRunnable implements \Jenner\SimpleFork\Runnable
{
/**
* @var \Jenner\SimpleFork\Lock\LockInterface
*/
protected $sem;
public function __construct()
{
$this->sem = \Jenner\SimpleFork\Lock\Semaphore::create("test");
//$this->sem = \Jenner\SimpleFork\Lock\FileLock::create("/tmp/test.lock");
}
/**
* @return mixed
*/
public function run()
{
for ($i = 0; $i < 20; $i++) {
$this->sem->acquire();
echo "my turn: {$i} " . getmypid() . PHP_EOL;
$this->sem->release();
sleep(1);
}
}
}
$pool = new \Jenner\SimpleFork\Pool();
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->wait();
$pool = new \Jenner\SimpleFork\Pool();
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->wait();
$fixed_pool = new \Jenner\SimpleFork\ParallelPool(new TestRunnable(), 10);
$fixed_pool->start();
$fixed_pool->keep(true);
$pool = new \Jenner\SimpleFork\FixedPool(2);
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->wait();
$pool = new \Jenner\SimpleFork\SinglePool();
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable()));
$pool->wait();