Implemented supervisors, refactored some stuff, implemented closures, updated examples and added dependency for Symfony\Process

This commit is contained in:
Netkas 2023-02-05 17:24:22 -05:00
parent 84b89eaf9d
commit 1b8d2fb40a
18 changed files with 707 additions and 190 deletions

View file

@ -1,57 +1,33 @@
<?php
use Tamer\Abstracts\Mode;
use Tamer\Abstracts\ProtocolType;
use Tamer\Objects\JobResults;
use Tamer\Objects\Task;
use Tamer\Tamer;
require 'ncc';
import('net.nosial.tamerlib', 'latest');
Tamer::connect(ProtocolType::Gearman, Mode::Client,
Tamer::init(ProtocolType::Gearman,
['127.0.0.1:4730']
);
// Pi calculation (closure)
// Add it 10 times
for($i = 0; $i < 100; $i++)
Tamer::addWorker(__DIR__ . '/tamer_worker.php', 10);
Tamer::startWorkers();
// Sleep function (task) loop 10 times
for ($i = 0; $i < 10; $i++)
{
Tamer::queueClosure(function() {
// Do Pi calculation
$pi = 0;
$top = 4.0;
$bot = 1.0;
$minus = true;
for($i = 0; $i < 1000000; $i++)
{
if($minus)
{
$pi -= ($top / $bot);
$minus = false;
}
else
{
$pi += ($top / $bot);
$minus = true;
}
$bot += 2.0;
}
\LogLib\Log::info('net.nosial.tamerlib', sprintf('Pi: %s', $pi));
return $pi;
});
Tamer::queue(Task::create('sleep', 5, function(JobResults $data)
{
echo "Slept for {$data->getData()} seconds \n";
}));
}
// Sleep function (task)
Tamer::queue(\Tamer\Objects\Task::create('sleep', 5, function(\Tamer\Objects\JobResults $data)
{
echo "Slept for {$data->getData()} seconds \n";
}));
echo "Waiting for jobs to finish \n";
$a = microtime(true);
Tamer::run();
$b = microtime(true);
echo "Took " . ($b - $a) . " seconds \n";