我正在处理流程到流程的沟通;目的是使工作进程执行一些计算,并将结果传回给控制进程.我安装了
zeromq.node,并在coffeescript中设置了一个简单的请求者和响应者.
@H_502_21@解决方法
请求者:
# 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)感觉不够快.根据this benchmark,我应该是每秒几十万个,而不是几千个消息,这是this discussion(“ZeroMQ:接收10,000个消息大约需要15毫秒”).
我也试图使用python 3编写的回复者,并得到完全相同的数字.此外,我写了一个替代的脚本,主进程将产生一个子进程,并通过stdout / stdin进行通信;我每秒获得大约750条消息(当我增加消息长度时,我看不到太多的差异),这与zeromq实验中的相同的一个ballpark.
这些数字是否预期?这里有什么限制因素?
我认为有两件事情正在发生.首先,你有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’,而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 ...