parallel\Channel::open

(0.9.0)

parallel\Channel::openAccess

说明

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

Shall open the channel with the given name

Exceptions

Warning

Shall throw parallel\Channel\Error\Existence if channel does not exist.

User Contributed Notes

gam6itko 25-Sep-2021 09:58
<?php

// example below shows how to get channel by name within child thread with 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);

// main thread
$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;
PHP8中文手册 站长在线 整理 版权归PHP文档组所有