略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: parallel\Channel::__construct

2024-11-14

parallel\Channel::__construct

(1.1.0)

parallel\Channel::__constructChannel Construction

说明

public parallel\Channel::__construct()

Shall make an anonymous unbuffered channel

public parallel\Channel::__construct(int $capacity)

Shall make an anonymous buffered channel with the given capacity

参数

capacity

May be Channel::Infinite or a positive integer

add a noteadd a note

User Contributed Notes 2 notes

up
1
abolfazl dot ziaratban at gmail dot com
1 year ago
$capacity is count of stack in buffer.

for example :

<?php
use parallel\{Channel,Runtime};

$test = function($ch){
   
var_dump($ch->Recv());
   
var_dump($ch->Recv());
   
var_dump($ch->Recv());
    echo
'Sleep'.PHP_EOL;
   
sleep(3);
    echo
'Call Recv()'.PHP_EOL;
   
var_dump($ch->Recv());
};

$t1 = new Runtime();
$ch = new Channel(3); //buffered channel
$ch->Send('abc1');
$ch->Send('abc2');
$ch->Send('abc3');
$t1->Run($test,[$ch]);
sleep(1);
echo
'Wait for Recv()...'.PHP_EOL;
$ch->Send('abc4'); #php waits for only one stack of channel to be empty(by Recv() method)
?>

result:
string(4) "abc1"
string(4) "abc2"
string(4) "abc3"
Sleep
Wait for Recv()...
Call Recv()
string(4) "abc4"
up
0
frame86 at live dot com
2 years ago
Please note $capacity unit is 1 MiB each, not bytes

官方地址:https://www.php.net/manual/en/parallel-channel.construct.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3