我正在运行 Swoole WebSocket 服务器,并且正在尝试在脚本运行时向最终用户发送消息。我遇到的问题是消息不是异步发送的,只有在脚本完成执行后才会发送。哪种方式违背了目的。
我的问题的精简版如下:
# Recipient IDs
$fd = [75371];
# Message to recipient
$message = "Ping";
# Let's send the message as a coroutine
go(function() use ($fd, $message){
print "About to send message [{$message}] to recipients ".json_encode($fd).PHP_EOL;
# Connect to the WebSocket and send the message
$client = new \Swoole\Coroutine\Http\Client(0.0.0.0, 1234);
$client->upgrade("/");
$client->push(json_encode([
"fd" => $fd,
"data" => $message
]));
$client->close();
print "Finally the message was sent".PHP_EOL;
});
# Coroutine script is done, let's continue with our other tasks
print "But wait, I'm gonna be busy with other tasks for 5 seconds first".PHP_EOL;
sleep(5); // This simulates process intensive tasks
# Another coroutine, not related
go(function() {
print "Gonna be busy again again for another 5 seconds".PHP_EOL;
co\System::sleep(5);
print "And we're done".PHP_EOL;
});
我的印象是协程中的命令将像单独的线程一样执行,我的意思是线程不会关心父脚本线程中发生的事情,因为它正在做自己的事情。
情况似乎并非如此。在发送消息之前,脚本会继续执行,直到我们完成下面的“任务”,消息才最终被发送。我期待的顺序如下:
About to send message [Ping] to recipients [75371]
Finally the message was sent
But wait, I'm gonna be busy with other tasks for 5 seconds first
Gonna be busy again again for another 5 seconds
And we're done
但我得到的顺序如下:
About to send message [Ping] to recipients [75371]
But wait, I'm gonna be busy with other tasks for 5 seconds first
Gonna be busy again again for another 5 seconds
Finally the message was sent
And we're done
我对协程有什么误解?我已经将协程作为一个实际的独立 PHP 线程来执行。然后它起作用了,但这似乎有点适得其反,因为我认为协程是线程。一切都必须在协程中才能工作吗?