问题
I have two simple components which are supposed to communicate with each other using the REQ/REP ZeroMQ pattern. The Server (REP Socket) is implemented in Python using pyzmq:
import zmq
def launch_server():
print "Launching server"
with zmq.Context.instance() as ctx:
socket = ctx.socket(zmq.REP)
socket.bind('tcp://127.0.0.1:5555')
while True:
msg = socket.recv()
print "EOM\n\n"
The Client (REQ socket) written in C# using the NetMQ library:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetMQ;
namespace PyNetMQTest
{
class Program
{
static void Main(string[] args)
{
string msg;
NetMQ.Sockets.RequestSocket socket = new NetMQ.Sockets.RequestSocket();
socket.Connect("tcp://127.0.0.1:5555");
for(int i=0; i<5; i++)
socket.SendFrame("test_"+i);
}
}
}
The Python Server implementation has been tested and works fine by talking to a REQ socket implemented using Python. But the C# REQ socket throws the following error within the first iteration of the loop and no messages reach the Server whatsoever:
An unhandled exception of type 'NetMQ.FiniteStateMachineException' occurred in NetMQ.dll Additional information: Req.XSend - cannot send another request
Stacktrace:
at NetMQ.Core.Patterns.Req.XSend(Msg& msg)
at NetMQ.Core.SocketBase.TrySend(Msg& msg, TimeSpan timeout, Boolean more)
at NetMQ.NetMQSocket.TrySend(Msg& msg, TimeSpan timeout, Boolean more)
at NetMQ.OutgoingSocketExtensions.Send(IOutgoingSocket socket, Msg& msg, Boolean more)
at NetMQ.OutgoingSocketExtensions.SendFrame(IOutgoingSocket socket, String message, Boolean more)
at PyNetMQTest.Program.Main(String[] args) in d:\users\emes\documents\visual studio 2015\Projects\PyNetMQ Test\PyNetMQTest\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
These are the first my first steps with ZMQ and the C# code is taken from the library documentation. What makes the code throw this error?
I am using:
- pyzmq 14.7
- NetMQ 3.3.3.4
- .NET 4.6
====================== SOLUTION ======================
As explained by @somdoron in his answer, th roor casue was that both sockets need to go the full cycle of send/receive before beeing able to be reused. As a matter of fact the REP socket implemented in python did not ever change it's state either so the error was in both, the python AND the C# code. Here is the fixed code:
REP Socket
import zmq
def launch_server():
print "Launching server"
with zmq.Context.instance() as ctx:
socket = ctx.socket(zmq.REP)
socket.bind('tcp://127.0.0.1:5555')
while True:
msg = socket.recv()
socket.send("reply to "+msg)
print "EOM\n\n"
REQ Socket
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetMQ;
namespace PyNetMQTest
{
class Program
{
static void Main(string[] args)
{
NetMQ.Sockets.RequestSocket socket = new NetMQ.Sockets.RequestSocket();
socket.Connect("tcp://127.0.0.1:5555");
string msg, reply;
while (true)
{
Console.WriteLine("Type message: ");
msg = Console.ReadLine();
Console.WriteLine("Sending : " + msg);
socket.SendFrame(msg);
reply = socket.ReceiveFrameString();
Console.WriteLine("Received: " + reply + Environment.NewLine);
}
}
}
}
回答1:
Request and Response sockets are state machines, with Request you must first Send and then call Receive, you cannot call 5 consecutive Send.
With Response its the opposite, you must call Receive first.
If one side is only sending and the other only receiving you can use Push-Pull pattern instead of Req-Rep. You can also use Dealer-Router if needed both ways communication. Anyway it seems the usage of Req-Rep is incorrect.
来源:https://stackoverflow.com/questions/39860614/zeromq-finitestatemachineexception-in-req-rep-pattern