innmind / ipc
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
通过使用Unix套接字进行消息传递来抽象进程间通信的库。
安装
composer require innmind/ipc
使用
# 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'), );
上述示例将在进程 B
中产生输出 server responded hello world
。
您可以在进程 A
停止之前运行进程 B
42
次。