1

I have a scenario where i have clients in .net that needs to connect to a python server that creates a thread for each accepted client. Server and client then send AND recieve data between them until the client disconnects and the specific client thread is closed.

Does this scenario fits ZeroMQ and if so, what patterns should i use to implement this scenario? (socket types etc.)

6
  • Thread per client doesn't sound good, especially if you take into consideration what Python does to simulate threads. Have you gone through the ZMQ guide? Commented Oct 29, 2015 at 9:03
  • Yes, i read the guide (at least most of it), and i know threads are not a good practice thing in zeromq but my server needs to open a socket for each client to a 3rd party service that has a proprietry protocol and sdk. Commented Oct 29, 2015 at 9:14
  • So why not use regular sockets? What would you achieve by using ZMQ here? It looks like you don't need disconnected-tcp approach ZMQ offers.. Commented Oct 29, 2015 at 9:23
  • i thought it'll save me writing all the send/receive logic for bridging python/.net strings and also partial message arrival. Commented Oct 29, 2015 at 9:40
  • Is the communication between client and server synchronous or asynchronous? If it's synchronous, give it a try with REQ - REP socket pairs. Commented Oct 29, 2015 at 9:57

1 Answer 1

1

Whether it works with ZMQ or not depends on how you intend to handle the sockets and threading on your server. It's entirely unclear whether you intend the server threads to handle the ZMQ communication, or if all of the ZMQ communication happens in a single thread, and the other thread-per-process communicates back to the main thread for ZMQ to send back to the client. The latter scenario would be the "ZMQ way", the former requires a few more hoops to work correctly.

If you want all ZMQ communication to happen in a single thread, then you just need to keep track of new clients as they connect, spin up a thread and the communication with that thread (which could happen over a separate pair of ZMQ sockets if you like), and then as the clients disconnect, spin down the thread. Fairly straight forward as an architecture.

If you'd prefer to have each thread handle ZMQ communication back to its client, things get a little hairy. You cannot have a single socket on the server be available to all threads, for each to use to communicate back to its client simultaneously. ZMQ sockets are not threadsafe. They are intended to be used by one thread at a time, serially, and it's recommended against passing them around among threads (but can be done by executing a full memory barrier - unsure what it takes to accomplish this in python, if it's possible; it's suggested here that the Python GIL may accomplish this "for free" - if you choose to rely on that behavior, good luck).

The way to do this the "ZMQ way" would be to accept an incoming connection in the main thread, spin up a thread with a new socket (it can use the same ZMQ context, which is threadsafe), communicate back to the client which new port is being bound on, have the client disconnect from the main socket and reconnect to the socket in the new thread. When it disconnects, have that thread terminate. A bit of a kludge, but workable if that's what you're after.

If either of these methods works for you, I'd use DEALER(client)/ROUTER(server) as your socket types to support fully asynchronous communication. Chapter 4 of the guide gives you some possible communication patterns to start with for various goals.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.