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

@@ -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();