我正在涉及进程间通信;目标是让工作进程执行一些计算并将结果传回控制进程。我安装了zeromq.node并在coffeescript中设置了一个简单的请求者和响应者。
请求者:
# requester.coffee
zmq = require 'zmq'
context = new zmq.Context()
socket = zmq.socket 'req'
socket.bind 'tcp://127.0.0.1:5555', ( error ) =>
throw error if error?
console.log 'Requesting writer bound to port 5555'
setInterval ( -> socket.send 'helo world' ), 1
response_count = 0
t0 = new Date() / 1000
socket.on 'message', ( message ) ->
response_count += 1
# x = message.toString 'utf-8'
if response_count % 1000 is 0
t1 = new Date() / 1000
console.log "received #{ parseInt response_count / ( t1 - t0 ) + 0.5 } messages per second"
response_count = 0
t0 = new Date() / 1000
响应者:
# responder.coffee
zmq = require 'zmq'
context = new zmq.Context()
socket = zmq.socket 'rep'
socket.connect 'tcp://127.0.0.1:5555'
console.log 'Responder bound to port 5555'
process.stdin.resume()
request_count = 0
t0 = new Date() / 1000
socket.on 'message', ( message ) ->
request_count += 1
# message = message.toString 'utf-8'
# console.log message
socket.send 'helo back'
if request_count % 1000 is 0
t1 = new Date() / 1000
console.log "received #{ parseInt request_count / ( t1 - t0 ) + 0.5 } messages per second"
request_count = 0
t0 = new Date() / 1000
现在当我在我的ubuntu(11.10,8GB,Intel Duo Core 3GHz,NodeJS 0.8.6)机器上的单独终端窗口中运行它时,我得到以下输出:
received 135 messages per second
received 6369 messages per second
received 6849 messages per second
received 6944 messages per second
received 7042 messages per second
received 7143 messages per second
received 5952 messages per second
received 2967 messages per second
received 914 messages per second
received 912 messages per second
received 928 messages per second
received 919 messages per second
received 947 messages per second
received 906 messages per second
received 918 messages per second
received 929 messages per second
received 916 messages per second
received 917 messages per second
received 916 messages per second
received 928 messages per second
其中(1)看起来有点像几秒后传输通道中存在某种饱和状态; (2)感觉不够快。根据{{3}},我应该是每秒数十万 - 而不是数千 - 的消息,这由this benchmark证实(“ZeroMQ:接收10,000条消息大约需要15毫秒”) 。
我还尝试使用python 3编写的响应程序并获得完全相同的数字。更重要的是,我编写了另一对脚本,其中主进程将生成子进程并通过stdout / stdin与它进行通信;我每秒获得大约750条消息(当我增加消息长度时,我看不出多少差异),这与zeromq实验的大致相同。
这些数字是否可以预期?这里的限制因素是什么?
答案 0 :(得分:7)
我认为有两件事正在发生。首先,你有setInterval ( -> socket.send 'helo world' ), 1
。这会每毫秒发送一个请求,因此您将被限制为每秒1000个请求。
此外,请求/响应模型是同步的。请求进入套接字并阻塞,直到给出响应。通过响应,处理并阻止下一个请求。
我将请求者脚本从socket = zmq.socket 'req'
更改为socket = zmq.socket 'push'
,将setInterval ( -> socket.send 'helo world' ), 0
更改为socket.send 'helo world' while true
。然后我将响应者脚本socket = zmq.socket 'rep'
更改为socket = zmq.socket 'pull'
并获得此输出
$ coffee responder.coffee
Responder bound to port 5555
received 282 messages per second
received 333357 messages per second
received 249988 messages per second
received 333331 messages per second
received 250003 messages per second
received 333331 messages per second
received 333331 messages per second
received 333331 messages per second
...
请求者没有输出,因为它在while循环中被阻塞,但它确实证明了ØMQ可以获得更好的性能。