Update README.md, consistent use of spaces instead of tabs, better examples
This commit is contained in:
11
bin/classes/Counter.php
Normal file
11
bin/classes/Counter.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
class Counter
|
||||
{
|
||||
public int $value = 0;
|
||||
|
||||
public function increment(): void
|
||||
{
|
||||
$this->value++;
|
||||
}
|
||||
}
|
||||
19
bin/classes/Job.php
Normal file
19
bin/classes/Job.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?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("* {$this->title}");
|
||||
sleep(1);
|
||||
print("\r {$this->title}\n");
|
||||
}
|
||||
}
|
||||
@@ -4,29 +4,21 @@ use Toalett\Multiprocessing\ContextBuilder;
|
||||
use Toalett\Multiprocessing\Task\Interval;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
require_once __DIR__ . '/classes/Counter.php';
|
||||
const NUM_JOBS = 50;
|
||||
|
||||
$counter = new class {
|
||||
public int $value = 0;
|
||||
|
||||
public function increment(): void
|
||||
{
|
||||
$this->value++;
|
||||
}
|
||||
};
|
||||
|
||||
$context = ContextBuilder::create()
|
||||
->withCleanupInterval(Interval::seconds(0.5))
|
||||
->build();
|
||||
->withCleanupInterval(Interval::seconds(0.5))
|
||||
->build();
|
||||
|
||||
$counter = new Counter();
|
||||
$context->on('worker_stopped', [$counter, 'increment']);
|
||||
$context->on('no_workers_remaining', [$context, 'stop']);
|
||||
$context->on('stopped', fn() => printf("\nJobs completed: %d\n", $counter->value));
|
||||
$context->on('stopped', fn() => printf(" %d\n", $counter->value));
|
||||
|
||||
for ($i = 0; $i < NUM_JOBS; $i++) {
|
||||
$context->submit(fn() => sleep(3));
|
||||
print('.');
|
||||
$context->submit(fn() => sleep(2));
|
||||
print('.');
|
||||
}
|
||||
|
||||
$context->run();
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Toalett\Multiprocessing\ConcurrencyLimit;
|
||||
use Toalett\Multiprocessing\ContextBuilder;
|
||||
use Toalett\Multiprocessing\Task\Interval;
|
||||
|
||||
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("+ {$this->title}");
|
||||
sleep(1);
|
||||
print("\r {$this->title}\n");
|
||||
}
|
||||
}
|
||||
|
||||
$limit = ConcurrencyLimit::singleWorker();
|
||||
$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', [$context, 'stop']);
|
||||
$context->run();
|
||||
21
bin/single_worker_with_job_class.php
Normal file
21
bin/single_worker_with_job_class.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
use Toalett\Multiprocessing\Concurrency;
|
||||
use Toalett\Multiprocessing\ContextBuilder;
|
||||
use Toalett\Multiprocessing\Task\Interval;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
require_once __DIR__ . '/classes/Job.php';
|
||||
|
||||
$context = ContextBuilder::create()
|
||||
->withConcurrency(Concurrency::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', [$context, 'stop']);
|
||||
$context->run();
|
||||
@@ -1,16 +1,16 @@
|
||||
<?php
|
||||
|
||||
use React\EventLoop\Factory;
|
||||
use Toalett\Multiprocessing\Concurrency;
|
||||
use Toalett\Multiprocessing\ContextBuilder;
|
||||
use Toalett\Multiprocessing\ConcurrencyLimit;
|
||||
use React\EventLoop\Factory as EventLoopFactory;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$loop = EventLoopFactory::create();
|
||||
$loop = Factory::create();
|
||||
$context = ContextBuilder::create()
|
||||
->withEventLoop($loop)
|
||||
->withLimit(ConcurrencyLimit::atMost(4))
|
||||
->build();
|
||||
->withEventLoop($loop)
|
||||
->withConcurrency(Concurrency::atMost(4))
|
||||
->build();
|
||||
|
||||
$context->on('booted', fn() => print("🚽 Toalett Multiprocessing Context\n"));
|
||||
$context->on('congestion', fn() => print('C'));
|
||||
Reference in New Issue
Block a user