parallel\Channel::open

(0.9.0)

parallel\Channel::open访问

描述

public parallel\Channel::open(string $name): Channel

将打开具有给定名称的通道

异常

警告

如果通道不存在,将抛出 parallel\Channel\Error\Existence

添加笔记

用户贡献的笔记 1 条笔记

gam6itko
2 年前
<?php

// 以下示例展示了如何在子线程中使用 Channel::open() 获取通道

use parallel\{Channel, Runtime};

$fnThread = static function () {
$channel = Channel::open('channel_name');
$message = $channel->recv();
echo
"- received message: $message\n";

return
'bye';
};

$channel = Channel::make('channel_name', 1);

// 主线程
$runtime = new Runtime();
$future = $runtime->run($fnThread, [$channel]);

echo
"sending message\n";
$channel->send('hello future!');
sleep(1);
echo
"closing channel\n";
$channel->close();

echo
"future said: ".$future->value();
echo
PHP_EOL;
To Top