-
Notifications
You must be signed in to change notification settings - Fork 57
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
leocavalcante
wants to merge
3
commits into
master
Choose a base branch
from
futures
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Introducing Futures #145
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: