Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
194 additions
and
65 deletions.
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
This file was deleted.
Oops, something went wrong.
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 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
require \dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
use Amp\Parallel\Worker; | ||
use Amp\Promise; | ||
|
||
$urls = [ | ||
'https://secure.php.net', | ||
'https://amphp.org', | ||
'https://github.com', | ||
]; | ||
|
||
$promises = []; | ||
foreach ($urls as $url) { | ||
$promises[$url] = Worker\enqueueCallable('file_get_contents', $url); | ||
} | ||
|
||
$responses = Promise\wait(Promise\all($promises)); | ||
|
||
foreach ($responses as $url => $response) { | ||
\printf("Read %d bytes from %s\n", \strlen($response), $url); | ||
} |
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
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,38 @@ | ||
<?php | ||
|
||
namespace Amp\Parallel\Worker; | ||
|
||
/** | ||
* Task implementation dispatching a simple callable. | ||
*/ | ||
final class CallableTask implements Task | ||
{ | ||
/** @var string */ | ||
private $callable; | ||
|
||
/** @var mixed[] */ | ||
private $args; | ||
|
||
/** | ||
* @param callable $callable Callable will be serialized. | ||
* @param mixed $args Arguments to pass to the function. Must be serializable. | ||
*/ | ||
public function __construct(callable $callable, array $args) | ||
{ | ||
$this->callable = $callable; | ||
$this->args = $args; | ||
} | ||
|
||
public function run(Environment $environment) | ||
{ | ||
if ($this->callable instanceof \__PHP_Incomplete_Class) { | ||
throw new \Error('When using a class instance as a callable, the class must be autoloadable'); | ||
} | ||
|
||
if (\is_array($this->callable) && ($this->callable[0] ?? null) instanceof \__PHP_Incomplete_Class) { | ||
throw new \Error('When using a class instance method as a callable, the class must be autoloadable'); | ||
} | ||
|
||
return ($this->callable)(...$this->args); | ||
} | ||
} |
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