Channel::sendAsync

(PHP 8.6+, True Async 1.0)

public Channel::sendAsync(mixed $value): bool

执行非阻塞的通道发送尝试。 与 send() 不同,此方法永远不会挂起协程。

如果值成功发送(放入缓冲区或交付给等待的接收方),返回 true。 如果缓冲区已满或通道已关闭,返回 false

参数

value
要发送的值。可以是任意类型。

返回值

true — 值已成功发送。 false — 通道已满或已关闭,值未被发送。

示例

示例 #1 尝试非阻塞发送

<?php

use Async\Channel;

$channel = new Channel(2);

$channel->sendAsync('a'); // true — 缓冲区为空
$channel->sendAsync('b'); // true — 有空间
$result = $channel->sendAsync('c'); // false — 缓冲区已满

echo $result ? "Sent" : "Channel full"; // "Channel full"

示例 #2 带可用性检查的发送

<?php

use Async\Channel;

$channel = new Channel(10);

spawn(function() use ($channel) {
    $data = generateBatch();

    foreach ($data as $item) {
        if (!$channel->sendAsync($item)) {
            // 缓冲区已满 — 回退到阻塞发送
            $channel->send($item);
        }
    }

    $channel->close();
});

参见