innmind / ipc
Inter-process communication
4.4.0
2024-03-10 14:33 UTC
Requires
- php: ~8.2
- innmind/immutable: ~4.15|~5.0
- innmind/media-type: ~2.0
- innmind/operating-system: ~5.0
- innmind/server-control: ~5.0
- innmind/socket: ~6.0
- innmind/url: ~4.0
Requires (Dev)
- innmind/black-box: ~5.5
- innmind/coding-standard: ~2.0
- nikic/php-parser: ^4.15.2
- phpunit/phpunit: ~10.2
- vimeo/psalm: ~5.15
README
Library to abstract the communication between processes by using message passing over unix sockets.
Installation
composer require innmind/ipc
Usage
# Process A use Innmind\IPC\{ Factory as IPC, Process\Name, }; use Innmind\OperatingSystem\Factory; $ipc = IPC::build(Factory::build()); $counter = $ipc->listen(Name::of('a'))(0, static function($message, $continuation, $counter): void { if ($counter === 42) { return $continuation->stop($counter); } return $continuation->respond($counter + 1, $message); })->match( static fn($counter) => $counter, static fn() => throw new \RuntimeException('Unable to start the server'), ); // $counter will always be 42 in this case
# Process B use Innmind\IPC\{ Factory as IPC, Process\Name, Message\Generic as Message, }; use Innmind\OperatingSystem\Factory; use Innmind\Immutable\Sequence; $ipc = IPC::build(Factory::build()); $server = Name::of('a'); $ipc ->wait(Name::of('a')) ->flatMap(fn($process) => $process->send(Sequence::of( Message::of('text/plain', 'hello world'), ))) ->flatMap(fn($process) => $process->wait()) ->match( static fn($message) => print('server responded '.$message->content()->toString()), static fn() => print('no response from the server'), );
The above example will result in the output server responded hello world
in the process B
.
You can run the process B
42
times before the server in the process A
stops.