Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Futures #145

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"src/constants.php",
"src/core/Coroutine/functions.php",
"src/core/Coroutine/Http/functions.php",
"src/core/Future/functions.php",
"src/std/exec.php",
"src/ext/curl.php",
"src/ext/sockets.php",
Expand Down
44 changes: 44 additions & 0 deletions examples/futures/docs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

use Swoole\Coroutine;
use Swoole\Coroutine\Http;
use Swoole\Future;

require_once __DIR__ . '/../bootstrap.php';

\Co\run(static function (): void {
$future = Future\async(static function (): void {
echo "are lazy\n";
});

Coroutine::create(static function (): void {
echo 'Futures ';
});

echo $future->await();

$future = Future\async(static function (): string {
return Http\get('https://httpbin.org/get')->getBody();
});

echo $future->await();

$future = Future\async(static function (): string {
throw new RuntimeException('Futures propagates exceptions');
});

try {
$future->await();
} catch (Throwable $e) {
echo $e->getMessage();
}
});
18 changes: 18 additions & 0 deletions examples/futures/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

require_once __DIR__ . '/../bootstrap.php';

\Swoole\Coroutine\run(static function () {
echo \Swoole\Future\async(static function () {
return \Swoole\Coroutine\Http\get('https://httpbin.org/get')->getBody();
})->await();
});
20 changes: 20 additions & 0 deletions examples/futures/future.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

require_once __DIR__ . '/../bootstrap.php';

\Swoole\Coroutine\run(static function () {
$future = \Swoole\Future::create(static function () {
return \Swoole\Coroutine\Http\get('https://httpbin.org/get')->getBody();
});

echo $future->await();
});
28 changes: 28 additions & 0 deletions examples/futures/join.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

require_once __DIR__ . '/../bootstrap.php';

\Swoole\Coroutine\run(static function () {
$start = microtime(true);

$future1 = \Swoole\Future::create(static function () {
return \Swoole\Coroutine\Http\get('https://httpbin.org/delay/2')->getBody();
});

$future2 = \Swoole\Future::create(static function () {
return \Swoole\Coroutine\Http\get('https://httpbin.org/delay/2')->getBody();
});

echo implode(PHP_EOL, \Swoole\Future::join([$future1, $future2]));

printf("Elapsed %f\n", microtime(true) - $start);
});
18 changes: 18 additions & 0 deletions examples/futures/throw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

require_once __DIR__ . '/../bootstrap.php';

\Swoole\Coroutine\run(static function () {
\Swoole\Future::create(static function () {
throw new RuntimeException('Futures exceptions are propagated');
})->await();
});
82 changes: 82 additions & 0 deletions src/core/Future.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole;

use Swoole\Coroutine\Channel;
use Throwable;

class Future
{
/**
* @var callable
*/
private $func;

public function __construct(callable $func)
{
$this->func = $func;
}

public static function create(callable $func): self
{
return new self($func);
}

public static function join(array $futures, float $timeout = -1): array

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your implementation is good and readable. But, I would put some minor tweaks (not functional ones, just a different style and one less variable to decrement) into it:

   public static function join(array $futures, float $timeout = -1): array
    {
        $ch = new Channel(count($futures));

        Coroutine::join(array_map(static function (Future $future) use ($ch): int {
            return $future->run($ch);
        }, $futures));

        $rets = [];
        while ($ret = $ch->pop($timeout)) {
            if ($ret instanceof Throwable) {
                throw $ret;
            }

            $rets[] = $ret;
        }

        return $rets;
    }

{
$len = count($futures);
$ch = new Channel($len);
$rets = [];

Coroutine::join(array_map(static function (Future $future) use ($ch): int {
return $future->run($ch);
}, $futures));

while ($len--) {
$ret = $ch->pop($timeout);

if ($ret instanceof Throwable) {
throw $ret;
}

$rets[] = $ret;
}

return $rets;
}

public function run(Channel $channel): int
{
return Coroutine::create(function () use ($channel) {
try {
$channel->push(($this->func)());
} catch (Throwable $throwable) {
$channel->push($throwable);
}
});
}

public function await(float $timeout = -1)
{
$ch = new Channel(1);

$cid = $this->run($ch);

$ret = $ch->pop($timeout);

if ($ret instanceof Throwable) {
throw $ret;
}

return $ret;
}
}
24 changes: 24 additions & 0 deletions src/core/Future/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Future;

use Swoole\Future;

function async(callable $func): Future
{
return Future::create($func);
}

function join(...$futures): array
{
return Future::join($futures);
}
51 changes: 51 additions & 0 deletions tests/unit/Future/FutureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Future;

use PHPUnit\Framework\TestCase;
use function Co\run;

/**
* @internal
* @coversNothing
*/
class FutureTest extends TestCase
{
public function testAwait(): void
{
run(function (): void {
$future = async(static function (): string {
return 'test';
});

$this->assertSame('test', $future->await());
});
}

public function testJoin(): void
{
run(function (): void {
$future1 = async(static function (): string {
return 'foo';
});

$future2 = async(static function (): string {
return 'bar';
});

$strings = join($future1, $future2);

$this->assertContains('foo', $strings);
$this->assertContains('bar', $strings);
});
}
}