innmind/ipc
Composer 安装命令:
composer require innmind/ipc
包简介
Inter-process communication
关键字:
README 文档
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\{ IPC, Process\Name, Continuation, Server, Message, }; use Innmind\OperatingSystem\Factory; use Innmind\Immutable\Monoid; /** * @psalm-immutable * @implements Monoid<int> */ final class Addition implements Monoid { public function identity(): int { return 0; } public function combine(mixed $a, mixed $b): int { return $a + $b; } } $ipc = IPC::build(Factory::build()); $counter = $ipc ->serve(Name::of('a')) ->sink(new Addition) ->monitor(static fn(int $counter, Server\Continuation $continuation) => match ($counter) { 42 => $continuation->finish(), default => $continuation, }) ->with( static fn(Message $message, Continuation $continuation, int $counter) => $continuation ->respond($message) ->carryWith($counter + 1), ) ->unwrap(); // $counter will always be 42 in this case
# Process B use Innmind\IPC\{ IPC, Process, Process\Name, Message, }; use Innmind\OperatingSystem\Factory; use Innmind\MediaType\{ MediaType, TopLevel, }; use Innmind\Immutable\{ Str, Sequence, }; $ipc = IPC::build(Factory::build()); $server = Name::of('a'); $response = $ipc ->connectTo($server) ->flatMap( static fn(Process $process) => $process ->send(Sequence::of( Message::of( MediaType::from(TopLevel::text, 'plain'), Str::of('hello world'), ), )) ->map(static fn() => $process), ) ->flatMap(fn(Process $process) => $process->wait()) ->match( static fn(Message $message) => 'server responded '.$message->content()->toString(), static fn() => 'no response from the server', ); print($message);
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.
统计信息
- 总下载量: 2.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-16