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

/*
 * This file is part of the async generator runtime project.
 *
 * (c) Julien Bianchi <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\pipe\{make};
use function jubianchi\async\runtime\{await, all, fork};
use function jubianchi\async\time\{delay, throttle};
$pipe = make();
$i = 0;
await(all(throttle(2500, function () use($pipe, &$i) {
    $pipe->enqueue($i++);
}), throttle(500, function () use($pipe) {
    echo __LINE__;
    var_dump("" . (yield from $pipe->dequeue()) . "");
}), throttle(1000, function () use($pipe) {
    echo __LINE__;
    var_dump("" . (yield from $pipe->dequeue()) . "");
})));
コード例 #2
0
<?php

/*
 * This file is part of the async generator runtime project.
 *
 * (c) Julien Bianchi <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\runtime\{await, all, wrap};
use function jubianchi\async\time\{delay};
$start = microtime(true);
function second()
{
    var_dump(__FUNCTION__ . ' - ' . 3);
    yield from delay(1000);
    var_dump(__FUNCTION__ . ' - ' . 4);
}
function first()
{
    var_dump(__FUNCTION__ . ' - ' . 1);
    yield from delay(1000);
    var_dump(__FUNCTION__ . ' - ' . 2);
    yield from delay(1000);
    yield from second();
    return 5;
}
var_dump(await(all(first(), second())));
echo 'Time spent: ' . ($with = microtime(true) - $start) . PHP_EOL;
コード例 #3
0
ファイル: all.php プロジェクト: jubianchi/async-generator
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\runtime\{await, all};
function producer($prefix, $length) : \generator
{
    for ($i = 0; $i < $length; $i++) {
        echo $prefix . '-' . $i . PHP_EOL;
        yield;
    }
    return $prefix . '-' . __LINE__;
}
var_dump(await(all(producer(__LINE__, 5), producer(__LINE__, 2))));
コード例 #4
0
ファイル: read.php プロジェクト: jubianchi/async-generator
const BUFFER_LENGTH = 2048;
$start = microtime(true);
$f1 = fopen(__DIR__ . '/../data/d1.dat', 'r');
$f2 = fopen(__DIR__ . '/../data/d2.dat', 'r');
$length = 0;
while ($buffer = fread($f1, BUFFER_LENGTH)) {
    $length += strlen($buffer);
}
while ($buffer = fread($f2, BUFFER_LENGTH)) {
    $length += strlen($buffer);
}
fclose($f1);
fclose($f2);
echo 'Found ' . $length . ' characters' . PHP_EOL;
echo 'Time spent without await: ' . ($without = microtime(true) - $start) . PHP_EOL;
$start = microtime(true);
$f1 = fopen(__DIR__ . '/../data/d1.dat', 'r');
$f2 = fopen(__DIR__ . '/../data/d2.dat', 'r');
$length = 0;
await(all(read($f1, function ($data) use(&$length) {
    $length += strlen($data);
}, null, BUFFER_LENGTH), read($f2, function ($data) use(&$length) {
    $length += strlen($data);
}, null, BUFFER_LENGTH)));
echo 'Found ' . $length . ' characters' . PHP_EOL;
echo 'Time spent with await: ' . ($with = microtime(true) - $start) . PHP_EOL;
if ($without > $with) {
    echo 'await is ' . round($without / $with, 2) . ' times faster' . PHP_EOL;
} else {
    echo 'await is ' . round($with / $without, 2) . ' times slower' . PHP_EOL;
}
コード例 #5
0
ファイル: http.php プロジェクト: jubianchi/async-generator
use function jubianchi\async\time\{delay};
use jubianchi\async\socket;
$address = '0.0.0.0';
$port = $_SERVER['argv'][1] ?? 1337;
$queue = [];
$start = function ($socket, $address, $port) use(&$queue) {
    socket_bind($socket, $address, $port);
    socket_listen($socket, 0);
    socket_set_nonblock($socket);
    $index = 0;
    $cancel = false;
    while ($cancel == false) {
        $client = socket_accept($socket);
        if ($client) {
            echo '> Got client...' . PHP_EOL;
            $queue[] = (function () use($index, $client, $address, $port) {
                echo '> Handling request #' . $index . '...' . PHP_EOL;
                $response = 'Hello World!';
                $output = 'HTTP/1.1 200 OK' . "\r\n" . 'Date: ' . date("D, j M Y G:i:s T") . "\r\n" . 'Server: AsyncGenerator/1.0.0 (PHP ' . phpversion() . ')' . "\r\n" . 'Content-Length: ' . strlen($response) . "\r\n" . 'Content-Type: text/plain' . "\r\n" . "\r\n" . $response . "\r\n";
                yield from delay(1000);
                yield from socket\write($client, $output, 5);
                socket_close($client);
            })();
            echo '> Client request #' . $index++ . ' queued...' . PHP_EOL;
        }
        $cancel = yield;
    }
    socket_close($socket);
};
await(race($start(socket_create(AF_INET, SOCK_STREAM, 0), $address, $port), fork($queue)));
コード例 #6
0
ファイル: some.php プロジェクト: jubianchi/async-generator
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\runtime\{await, some};
function producer($prefix, $length) : \generator
{
    $cancel = false;
    for ($i = 0; $i < $length && $cancel === false; $i++) {
        echo $prefix . '-' . $i . PHP_EOL;
        $cancel = (bool) yield;
    }
    if ($cancel === true) {
        echo $prefix . '-canceled' . PHP_EOL;
    }
    return $prefix . '-' . __LINE__;
}
var_dump(await(some(2, producer(__LINE__, 5), producer(__LINE__, 10), producer(__LINE__, 2))));
コード例 #7
0
<?php

/*
 * This file is part of the async generator runtime project.
 *
 * (c) Julien Bianchi <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once __DIR__ . '/../vendor/autoload.php';
use function jubianchi\async\runtime\{await, race};
use function jubianchi\async\time\{delay, throttle};
$start = microtime(true);
await(race(delay(3000), throttle(500, function () {
    var_dump(__LINE__);
}), throttle(1000, function () {
    var_dump(__LINE__);
})));
echo 'Time spent: ' . ($with = microtime(true) - $start) . PHP_EOL;
コード例 #8
0
ファイル: socket.php プロジェクト: jubianchi/async-generator
<?php

/*
 * This file is part of the async generator runtime project.
 *
 * (c) Julien Bianchi <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\loop\{endless};
use function jubianchi\async\pipe\{make};
use function jubianchi\async\runtime\{await, all};
use function jubianchi\async\socket\{write};
use function jubianchi\async\stream\{tail};
$pipe = make();
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, 0, $argv[1]);
socket_set_nonblock($socket);
await(all(tail(fopen(__DIR__ . '/../data/first.log', 'r'), $pipe), tail(fopen(__DIR__ . '/../data/second.log', 'r'), $pipe), endless(function () use($socket, $pipe) {
    $data = (yield from $pipe->dequeue());
    yield from write($socket, $data);
})));
コード例 #9
0
ファイル: wrap.php プロジェクト: jubianchi/async-generator
 * This file is part of the async generator runtime project.
 *
 * (c) Julien Bianchi <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once __DIR__ . '/../vendor/autoload.php';
use function jubianchi\async\runtime\{await, all, wrap};
use function jubianchi\async\time\{delay};
$start = microtime(true);
function two()
{
    (yield 3);
    (yield 4);
}
function five()
{
    (yield 1);
    (yield 2);
    yield from two();
    return 5;
}
$generators = await(all(wrap(five()), wrap(two())));
foreach ($generators as $generator) {
    foreach ($generator as $v) {
        var_dump($v);
    }
    var_dump('return: ' . $generator->getReturn());
}
echo 'Time spent: ' . ($with = microtime(true) - $start) . PHP_EOL;
コード例 #10
0
ファイル: delay.php プロジェクト: jubianchi/async-generator
<?php

//index.php
require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\runtime\{await, all};
use function jubianchi\async\time\{delay};
$start = microtime(true);
await(all(delay(5000, function () {
    echo 'World!';
}), delay(2000, function () {
    echo 'Hello';
})));
echo PHP_EOL . 'Time spent: ' . ($with = microtime(true) - $start) . PHP_EOL;
コード例 #11
0
ファイル: await.php プロジェクト: jubianchi/async-generator
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\runtime\{await};
function producer($prefix, $length) : \generator
{
    for ($i = 0; $i < $length; $i++) {
        echo $prefix . '-' . $i . PHP_EOL;
        yield;
    }
    return __LINE__;
}
var_dump(await(producer(__LINE__, 5)));
コード例 #12
0
ファイル: race.php プロジェクト: jubianchi/async-generator
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use function jubianchi\async\runtime\{await, race};
function producer($prefix, $length) : \generator
{
    $cancel = false;
    for ($i = 0; $i < $length && $cancel === false; $i++) {
        echo $prefix . '-' . $i . PHP_EOL;
        $cancel = (bool) yield;
    }
    if ($cancel === true) {
        echo $prefix . '-canceled' . PHP_EOL;
    }
    return $prefix . '-' . __LINE__;
}
var_dump(await(race(producer(__LINE__, 5), producer(__LINE__, 2))));
コード例 #13
0
<?php

/*
 * This file is part of the async generator runtime project.
 *
 * (c) Julien Bianchi <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once __DIR__ . '/../vendor/autoload.php';
use function jubianchi\async\runtime\{await, all};
use function jubianchi\async\stream\{read};
var_dump(await(all(read(fopen(__DIR__ . '/data/tiny.dat', 'r'), function ($d) {
    var_dump($d);
}, 1), read(fopen(__DIR__ . '/data/d1.dat', 'r'), function ($d) {
    var_dump($d);
}, 10))));