[英]Correct pattern for ZeroMQ Server/Client type
I have a server that needs to get instructions to run processes for clients on another machine. 我有一台服务器,该服务器需要获取有关在另一台计算机上为客户端运行进程的指令。
The clients send a job message, the Server processes the job and later sends the back results. 客户端发送作业消息,服务器处理作业,然后发送回结果。
I tried using the NetMQ Request-Response pattern (see below) 我尝试使用NetMQ请求-响应模式(请参见下文)
This works nicely for 1 client, BUT if a second client sends a request before previous client job is finished - I get an error. 这对于1个客户端来说效果很好,但如果第二个客户端在上一个客户端作业完成之前发送了请求,则会出错-我收到错误消息。
I really need to be able to receive ad-hoc messages from clients, and send results when they are completed. 我确实需要能够接收来自客户端的临时消息,并在完成后发送结果。 Clearly, I am using the wrong pattern, but reading the ZeroMQ docs has not highlighted a more appropriate one. 显然,我使用了错误的模式,但是阅读ZeroMQ文档并没有突出显示一个更合适的文档。
namespace Utils.ServerMQ
{
class ServerMQ
{
public static void Go()
{
using (var responseSocket = new ResponseSocket("@tcp://*:393"))
{
while (true)
{
Console.WriteLine("Server waiting");
var message = responseSocket.ReceiveFrameString();
Console.WriteLine("Server Received '{0}'", message);
//System.Threading.Thread.Sleep(1000);
var t2 = Task.Factory.StartNew(() =>
{
RunProcMatrix(message, responseSocket);
});
}
}
}
public static void RunProcMatrix(object state, ResponseSocket responseSocket)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Path.Combine(@"H:\Projects\Matrix\Matrix\bin\Debug\", "Matrix001.exe"),
Arguments = (string)state,
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false
}
};
process.Start();
process.WaitForExit();
responseSocket.SendFrame((string)state);
}
}
}
You want a ROUTER socket on the server side, so it can receive multiple requests at a time. 您需要在服务器端使用ROUTER套接字,以便它一次可以接收多个请求。 ( Guide ) REQ sockets on the client side are still fine unless the server may arbitrarily push data to them, then they need to be DEALER sockets. ( 指南 )除非服务器可以将数据任意推送到客户端,否则客户端的REQ套接字仍然可以使用,因此它们必须是DEALER套接字。
Note that for sockets beyond REQ/RESP you need to manually handle the message envelope (the first frame of the message indicating its destination). 请注意,对于超出REQ / RESP的套接字,您需要手动处理消息信封(消息的第一帧指示其目的地)。 Guide 指南
The 0MQ docs are incredibly dense... I don't blame you for not intuiting this from them :) 0MQ文档非常密集...我不怪你没有从他们那里领悟到这一点:)
This example from the NetMQ docs is full ROUTER-DEALER: https://netmq.readthedocs.io/en/latest/router-dealer/#router-dealer , you can take just the router side and it should work the same though. NetMQ文档中的此示例是完整的ROUTER-DEALER: https ://netmq.readthedocs.io/zh-CN/latest/router-dealer/#router-dealer,您可以只使用路由器端,但是应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.