Extract components and add more tests

This commit is contained in:
2020-12-12 02:11:05 +01:00
parent 600f52567f
commit 92bc0ab407
22 changed files with 598 additions and 149 deletions

View File

@@ -2,6 +2,7 @@
use Toalett\Multiprocessing\ConcurrencyLimit;
use Toalett\Multiprocessing\ContextBuilder;
use Toalett\Multiprocessing\Task\Interval;
require_once __DIR__ . '/../vendor/autoload.php';
@@ -16,20 +17,23 @@ class Job
public function __invoke()
{
cli_set_process_title('php ' . $this->title);
print("start:{$this->title}\n");
sleep(3);
print("stop :{$this->title}\n");
cli_set_process_title("php {$this->title}");
print("+ {$this->title}");
sleep(1);
print("\r {$this->title}\n");
}
}
$limit = ConcurrencyLimit::singleWorker();
$context = ContextBuilder::create()->withLimit($limit)->build();
$context = ContextBuilder::create()
->withLimit(ConcurrencyLimit::singleWorker())
->withCleanupInterval(Interval::seconds(0.2))
->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->on('no_workers_remaining', [$context, 'stop']);
$context->run();

View File

@@ -6,10 +6,11 @@ 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 = ContextBuilder::create()
->withEventLoop($loop)
->withLimit(ConcurrencyLimit::atMost(4))
->build();
$context->on('booted', fn() => print("🚽 Toalett Multiprocessing Context\n"));
$context->on('congestion', fn() => print('C'));
@@ -17,7 +18,6 @@ $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");

View File

@@ -1,10 +1,10 @@
<?php
use Toalett\Multiprocessing\ContextBuilder;
use Toalett\Multiprocessing\Task\Interval;
require_once __DIR__ . '/../vendor/autoload.php';
// We will run 50 jobs
const NUM_JOBS = 50;
$counter = new class {
@@ -16,22 +16,17 @@ $counter = new class {
}
};
// Create a default context with unlimited concurrency
$context = ContextBuilder::create()->build();
$context = ContextBuilder::create()
->withCleanupInterval(Interval::seconds(0.5))
->build();
// Each time a worker stops, a job is finished
$context->on('worker_stopped', fn() => $counter->increment());
$context->on('worker_stopped', [$counter, 'increment']);
$context->on('no_workers_remaining', [$context, 'stop']);
$context->on('stopped', fn() => printf("\nJobs completed: %d\n", $counter->value));
// 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));
print('.');
}
$context->run();