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();
|
||||
Reference in New Issue
Block a user