コード例 #1
0
ファイル: multi-generator.php プロジェクト: jderusse/async
<?php

if (file_exists($autoloader_path = __DIR__ . '/../vendor/autoload.php')) {
    require_once $autoloader_path;
} else {
    echo 'You did not install the composer dependency.' . PHP_EOL . 'Run composer install or go on ' . PHP_EOL . 'http://melody.sensiolabs.org/#btn_installation' . PHP_EOL . 'for more information on how to install melody.' . PHP_EOL;
    exit(1);
}
function foo($j)
{
    for ($i = 0; $i <= $j; $i++) {
        (yield $i . '/' . $j);
    }
}
$m = new \Async\MultiTasks(2);
for ($i = 0; $i < 5; $i++) {
    $m->addTask(new \Async\Adapter\Generator(foo($i)));
}
$m->start()->progress(function (\Async\Event\Event $event) {
    echo get_class($event) . ' ';
    if ($event instanceof \Async\Event\MessageEvent) {
        echo $event->getMessage();
    }
    echo PHP_EOL;
})->always(function () use(&$finished) {
    $finished = true;
});
while (!$finished) {
    $m->tick();
}
コード例 #2
0
ファイル: multi-process.php プロジェクト: jderusse/async
<?php

if (file_exists($autoloader_path = __DIR__ . '/../vendor/autoload.php')) {
    require_once $autoloader_path;
} else {
    echo 'You did not install the composer dependency.' . PHP_EOL . 'Run composer install or go on ' . PHP_EOL . 'http://melody.sensiolabs.org/#btn_installation' . PHP_EOL . 'for more information on how to install melody.' . PHP_EOL;
    exit(1);
}
$m = new \Async\MultiTasks(3);
for ($i = 0; $i < 5; $i++) {
    $m->addTask(new \Async\Adapter\SymfonyProcess(new \Symfony\Component\Process\Process('php -r "usleep(mt_rand(0, 100000)); echo ' . $i . ';"')));
}
$m->start()->progress(function (\Async\Event\Event $event) {
    echo get_class($event) . ' ';
    if ($event instanceof \Async\Event\MessageEvent) {
        echo $event->getMessage();
    }
    echo PHP_EOL;
})->always(function () use(&$finished) {
    $finished = true;
});
while (!$finished) {
    $m->tick();
}
コード例 #3
0
ファイル: multi-mixte.php プロジェクト: jderusse/async
    exit(1);
}
function foo($j)
{
    for ($i = 0; $i <= $j; $i++) {
        (yield 'generator:' . $i . '/' . $j);
    }
}
$mGenerator = new \Async\MultiTasks(2);
for ($i = 0; $i < 5; $i++) {
    $mGenerator->addTask(new \Async\Adapter\Generator(foo($i)));
}
$mProcess = new \Async\MultiTasks(3);
for ($i = 0; $i < 5; $i++) {
    $mProcess->addTask(new \Async\Adapter\SymfonyProcess(new \Symfony\Component\Process\Process('php -r "usleep(mt_rand(0, 100000)); echo \'process: ' . $i . '\';"')));
}
$mMixte = new \Async\MultiTasks();
$mMixte->addTask($mGenerator);
$mMixte->addTask($mProcess);
$mMixte->start()->progress(function (\Async\Event\Event $event) {
    echo get_class($event) . ' ';
    if ($event instanceof \Async\Event\MessageEvent) {
        echo $event->getMessage();
    }
    echo PHP_EOL;
})->always(function () use(&$finished) {
    $finished = true;
});
while (!$finished) {
    $mMixte->tick();
}