1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-12 17:17:20 +01:00
parallel/examples/parcel-process.php

36 lines
1.0 KiB
PHP
Raw Normal View History

2017-12-13 06:38:23 +01:00
<?php
// The function returned by this script is run by shared-memory-process.php in a separate process.
// $argc and $argv are available in this process as any other cli PHP script.
use Amp\Delayed;
use Amp\Parallel\Sync\Channel;
use Amp\Parallel\Sync\SharedMemoryParcel;
return function (Channel $channel) use ($argv): \Generator {
if (!isset($argv[1])) {
throw new \Error("No parcel ID provided");
}
$id = $argv[1];
2018-10-07 16:50:45 +02:00
\printf("Child process using parcel ID %s\n", $id);
2017-12-13 06:38:23 +01:00
$parcel = SharedMemoryParcel::use($id);
$value = yield $parcel->synchronized(function (int $value) {
return $value + 1;
});
2018-10-07 16:50:45 +02:00
\printf("Value after modifying in child thread: %s\n", $value);
2017-12-13 06:38:23 +01:00
yield new Delayed(500); // Parent process should access parcel during this time.
// Unwrapping the parcel now should give value from parent process.
2018-10-07 16:50:45 +02:00
\printf("Value in child thread after being modified in main thread: %s\n", yield $parcel->unwrap());
2017-12-13 06:38:23 +01:00
yield $parcel->synchronized(function (int $value) {
return $value + 1;
});
};