【发布时间】:2013-11-18 12:01:48
【问题描述】:
我在如下服务器上使用 websocket。它响应onmessage 事件,并根据消息条件执行不同的任务:
require "websocket-eventmachine-server"
WebSocket::EventMachine::Server.start(host: some_server_name, port: some_port) do |ws|
# (A) Here, the current thread is the main thread
ws.onmessage do |s|
if foo
# (B) Here, the current thread is the main thread
...
else
# (C) Here, the current thread is the main thread
...
end
end
end
每个onmessage事件执行的线程(上面描述为B和C)每次都是相同的,它们与主线程相同(上面描述为A)。
我想以C 的形式在单独的线程中执行B 中的代码。一种方法是将B 和C 中的操作放在一个新线程中,如下所示:
WebSocket::EventMachine::Server.start(host: some_server_name, port: some_port) do |ws|
# (A) Here, the current thread is the main thread
ws.onmessage do |s|
if foo
# (B) Here, the current thread will be created each time.
Thread.new{...}
else
# (C) Here, the current thread will be created each time.
Thread.new{...}
end
end
end
但是每次发生事件时创建一个新线程似乎很繁重,并且使响应变慢。所以,我希望在B 中处理的所有onmessage 事件之间共享一个线程,在C 中处理的所有事件之间共享另一个线程:
WebSocket::EventMachine::Server.start(host: some_server_name, port: some_port) do |ws|
# (A) Here, the current thread is the main thread
ws.onmessage do |s|
if foo
# (B) I want this part to be executed in a thread
# that does not change each time, but is different from the thread in C
...
else
# (C) I want this part to be executed in a thread
# that does not change each time, but is different from the thread in B
...
end
end
end
有什么好的方法可以做到这一点?或者,是否有更好的结构来以相互非阻塞的方式响应 websocket onmessage 事件?
【问题讨论】:
标签: ruby multithreading websocket eventmachine em-websocket