Implement Unit Tests, add Readme, add examples, stronger implementation
This commit is contained in:
35
bin/example_with_job_class.php
Normal file
35
bin/example_with_job_class.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Toalett\Multiprocessing\ConcurrencyLimit;
|
||||
use Toalett\Multiprocessing\ContextBuilder;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
class Job
|
||||
{
|
||||
private string $title;
|
||||
|
||||
public function __construct(string $title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function __invoke()
|
||||
{
|
||||
cli_set_process_title('php ' . $this->title);
|
||||
print("start:{$this->title}\n");
|
||||
sleep(3);
|
||||
print("stop :{$this->title}\n");
|
||||
}
|
||||
}
|
||||
|
||||
$limit = ConcurrencyLimit::singleWorker();
|
||||
$context = ContextBuilder::create()->withLimit($limit)->build();
|
||||
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$title = md5(mt_rand());
|
||||
$context->submit(new Job($title));
|
||||
}
|
||||
|
||||
$context->on('no_workers_remaining', fn() => $context->stop());
|
||||
$context->run();
|
||||
24
bin/more_elaborate_example.php
Normal file
24
bin/more_elaborate_example.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Toalett\Multiprocessing\ContextBuilder;
|
||||
use Toalett\Multiprocessing\ConcurrencyLimit;
|
||||
use React\EventLoop\Factory as EventLoopFactory;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Create our own EventLoop and limit and supply them to the builder
|
||||
$loop = EventLoopFactory::create();
|
||||
$limit = new ConcurrencyLimit(4);
|
||||
$context = ContextBuilder::create()->withEventLoop($loop)->withLimit($limit)->build();
|
||||
|
||||
$context->on('booted', fn() => print("🚽 Toalett Multiprocessing Context\n"));
|
||||
$context->on('congestion', fn() => print('C'));
|
||||
$context->on('congestion_relieved', fn() => print('R'));
|
||||
$context->on('worker_started', fn() => print('+'));
|
||||
$context->on('worker_stopped', fn() => print('-'));
|
||||
|
||||
// Submit a fake job every second
|
||||
$loop->addPeriodicTimer(1, fn() => $context->submit(fn(int $s) => sleep($s), random_int(0, 10)));
|
||||
|
||||
print("Press CTRL+C to stop.\n");
|
||||
$context->run();
|
||||
37
bin/simple_example.php
Normal file
37
bin/simple_example.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Toalett\Multiprocessing\ContextBuilder;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// We will run 50 jobs
|
||||
const NUM_JOBS = 50;
|
||||
|
||||
$counter = new class {
|
||||
public int $value = 0;
|
||||
|
||||
public function increment(): void
|
||||
{
|
||||
$this->value++;
|
||||
}
|
||||
};
|
||||
|
||||
// Create a default context with unlimited concurrency
|
||||
$context = ContextBuilder::create()->build();
|
||||
|
||||
// Each time a worker stops, a job is finished
|
||||
$context->on('worker_stopped', fn() => $counter->increment());
|
||||
|
||||
// Automatically stop the context when there are no workers left
|
||||
$context->on('no_workers_remaining', fn() => $context->stop());
|
||||
$context->on('stopped', fn() => printf("Jobs completed: %d\n", $counter->value));
|
||||
|
||||
// You can submit jobs before the context is running. They will be executed
|
||||
// in the order in which they are submitted to the context. They are
|
||||
// scheduled on a future tick of the underlying event loop.
|
||||
// Each job will involve sleeping for ~3 seconds in this example.
|
||||
for ($i = 0; $i < NUM_JOBS; $i++) {
|
||||
$context->submit(fn() => sleep(3));
|
||||
}
|
||||
|
||||
$context->run();
|
||||
Reference in New Issue
Block a user