1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00

Update examples

This commit is contained in:
Aaron Piotrowski 2018-10-21 10:34:08 -05:00
parent e72a50dc8d
commit 79a4f979f4
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
6 changed files with 17 additions and 5 deletions

View File

@ -1,7 +1,6 @@
<?php
// The function returned by this script is run by process.php in a separate process.
// echo, print, printf, etc. in this script are written to STDERR of the parent.
// $argc and $argv are available in this process as any other cli PHP script.
use Amp\Parallel\Sync\Channel;

View File

@ -1,7 +1,6 @@
<?php
// The function returned by this script is run by shared-memory-process.php in a separate process.
// echo, print, printf, etc. in this script are written to STDERR of the parent.
// $argc and $argv are available in this process as any other cli PHP script.
use Amp\Delayed;

View File

@ -2,6 +2,7 @@
<?php
require \dirname(__DIR__).'/vendor/autoload.php';
use Amp\ByteStream;
use Amp\Delayed;
use Amp\Loop;
use Amp\Parallel\Context\Process;
@ -19,6 +20,9 @@ Loop::run(function () {
\assert($context instanceof Process);
// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), new ByteStream\ResourceOutputStream(STDOUT)));
print "Waiting 2 seconds to send start data...\n";
yield new Delayed(2000);

View File

@ -2,6 +2,7 @@
<?php
require \dirname(__DIR__).'/vendor/autoload.php';
use Amp\ByteStream;
use Amp\Delayed;
use Amp\Loop;
use Amp\Parallel\Context\Process;
@ -11,11 +12,16 @@ Loop::run(function () {
// Create a parcel that then can be accessed in any number of child processes.
$parcel = SharedMemoryParcel::create($id = \bin2hex(\random_bytes(10)), 1);
$context = Process::run([
$context = yield Process::run([
__DIR__ . "/parcel-process.php",
$id, // Send parcel ID to child process as command argument.
]);
\assert($context instanceof Process);
// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), new ByteStream\ResourceOutputStream(STDOUT)));
yield new Delayed(100); // Give the process time to start and access the parcel.
yield $parcel->synchronized(function (int $value) {

View File

@ -16,7 +16,7 @@ Loop::run(function () {
try {
// Create a new child thread that does some blocking stuff.
$context = Thread::run(function (Channel $channel): \Generator {
$context = yield Thread::run(function (Channel $channel): \Generator {
\printf("Received the following from parent: %s\n", yield $channel->receive());
print "Sleeping for 3 seconds...\n";
@ -30,6 +30,8 @@ Loop::run(function () {
return 42;
});
\assert($context instanceof Thread);
print "Waiting 2 seconds to send start data...\n";
yield new Delayed(2000);

View File

@ -12,7 +12,7 @@ use Amp\Parallel\Sync\ThreadedParcel;
Loop::run(function () {
$parcel = new ThreadedParcel(1);
$context = Thread::run(function (Channel $channel, Parcel $parcel) {
$context = yield Thread::run(function (Channel $channel, Parcel $parcel) {
$value = yield $parcel->synchronized(function (int $value) {
return $value + 1;
});
@ -29,6 +29,8 @@ Loop::run(function () {
});
}, $parcel);
\assert($context instanceof Thread);
yield new Delayed(100); // Give the thread time to start and access the parcel.
yield $parcel->synchronized(function (int $value) {