mirror of
https://github.com/danog/parallel.git
synced 2025-01-22 14:01:14 +01:00
Update examples with BlockingTask
This commit is contained in:
parent
8625e5968e
commit
8695d600fc
@ -43,6 +43,7 @@
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Icicle\\Benchmarks\\Concurrent\\": "benchmarks",
|
||||
"Icicle\\Examples\\Concurrent\\": "examples",
|
||||
"Icicle\\Tests\\Concurrent\\": "tests"
|
||||
}
|
||||
}
|
||||
|
36
examples/BlockingTask.php
Normal file
36
examples/BlockingTask.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace Icicle\Examples\Concurrent;
|
||||
|
||||
use Icicle\Concurrent\Worker\Environment;
|
||||
use Icicle\Concurrent\Worker\TaskInterface;
|
||||
|
||||
class BlockingTask implements TaskInterface
|
||||
{
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $function;
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private $args;
|
||||
|
||||
/**
|
||||
* @param callable $function Do not use a closure or non-serializable object.
|
||||
* @param mixed ...$args Arguments to pass to the function. Must be serializable.
|
||||
*/
|
||||
public function __construct(callable $function /* ...$args */)
|
||||
{
|
||||
$this->function = $function;
|
||||
$this->args = array_slice(func_get_args(), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run(Environment $environment)
|
||||
{
|
||||
return call_user_func_array($this->function, $this->args);
|
||||
}
|
||||
}
|
@ -2,23 +2,33 @@
|
||||
<?php
|
||||
require dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
use Icicle\Concurrent\Worker;
|
||||
use Icicle\Concurrent\Worker\HelloTask;
|
||||
use Icicle\Concurrent\Worker\Pool;
|
||||
use Icicle\Coroutine\Coroutine;
|
||||
use Icicle\Examples\Concurrent\BlockingTask;
|
||||
use Icicle\Loop;
|
||||
use Icicle\Promise;
|
||||
|
||||
$generator = function () {
|
||||
$returnValues = (yield Promise\all([
|
||||
new Coroutine(Worker\enqueue(new HelloTask())),
|
||||
new Coroutine(Worker\enqueue(new HelloTask())),
|
||||
new Coroutine(Worker\enqueue(new HelloTask())),
|
||||
$pool = new Pool();
|
||||
$pool->start();
|
||||
|
||||
$results = (yield Promise\all([
|
||||
'google.com' => new Coroutine($pool->enqueue(new BlockingTask('file_get_contents', 'https://google.com'))),
|
||||
'icicle.io' => new Coroutine($pool->enqueue(new BlockingTask('file_get_contents', 'https://icicle.io'))),
|
||||
]));
|
||||
|
||||
var_dump($returnValues);
|
||||
foreach ($results as $source => $result) {
|
||||
printf("Read from %s: %d bytes\n", $source, strlen($result));
|
||||
}
|
||||
|
||||
yield $pool->shutdown();
|
||||
};
|
||||
|
||||
$coroutine = new Coroutine($generator());
|
||||
$coroutine->done();
|
||||
|
||||
Loop\periodic(0.1, function () {
|
||||
printf(".\n");
|
||||
})->unreference();
|
||||
|
||||
Loop\run();
|
||||
|
@ -1,21 +0,0 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
require dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
use Icicle\Concurrent\Worker\HelloTask;
|
||||
use Icicle\Concurrent\Worker\WorkerProcess;
|
||||
use Icicle\Coroutine;
|
||||
use Icicle\Loop;
|
||||
|
||||
Coroutine\create(function () {
|
||||
$worker = new WorkerProcess();
|
||||
$worker->start();
|
||||
|
||||
$returnValue = (yield $worker->enqueue(new HelloTask()));
|
||||
printf("Return value: %s\n", $returnValue);
|
||||
|
||||
$code = (yield $worker->shutdown());
|
||||
printf("Code: %d\n", $code);
|
||||
})->done();
|
||||
|
||||
Loop\run();
|
@ -1,21 +0,0 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
require dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
use Icicle\Concurrent\Worker\HelloTask;
|
||||
use Icicle\Concurrent\Worker\WorkerThread;
|
||||
use Icicle\Coroutine;
|
||||
use Icicle\Loop;
|
||||
|
||||
Coroutine\create(function () {
|
||||
$worker = new WorkerThread();
|
||||
$worker->start();
|
||||
|
||||
$returnValue = (yield $worker->enqueue(new HelloTask()));
|
||||
printf("Return value: %s\n", $returnValue);
|
||||
|
||||
$code = (yield $worker->shutdown());
|
||||
printf("Code: %d\n", $code);
|
||||
})->done();
|
||||
|
||||
Loop\run();
|
23
examples/worker.php
Executable file
23
examples/worker.php
Executable file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
require dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
use Icicle\Concurrent\Worker\WorkerFactory;
|
||||
use Icicle\Coroutine;
|
||||
use Icicle\Examples\Concurrent\BlockingTask;
|
||||
use Icicle\Loop;
|
||||
|
||||
Coroutine\create(function () {
|
||||
$factory = new WorkerFactory();
|
||||
|
||||
$worker = $factory->create();
|
||||
$worker->start();
|
||||
|
||||
$result = (yield $worker->enqueue(new BlockingTask('file_get_contents', 'https://google.com')));
|
||||
printf("Read %d bytes\n", strlen($result));
|
||||
|
||||
$code = (yield $worker->shutdown());
|
||||
printf("Code: %d\n", $code);
|
||||
})->done();
|
||||
|
||||
Loop\run();
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
namespace Icicle\Concurrent\Worker;
|
||||
|
||||
class HelloTask implements TaskInterface
|
||||
{
|
||||
public function run(Environment $environment)
|
||||
{
|
||||
return "Hello, world!";
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user