问题
I want one application on a Linux(Server) host to communicate with applications on Win7(Client) in a VM. Lib of choice is ZeroMQ. But how do I manage asynchronous tasks?
Let me give an example: The application in the VM generates tasks in arbitrary intervals and sends them to the Linux box. This will process them but needs some time to answer. In this time the socket in REQ/REP pattern is blocked and incoming tasks from the WinApp can not be forwarded to the LinuxApp. How should I solve this? How is this generally solved, even without 0MQ. Do I have to make both to servers and clients and establish two connections?
Requirements:
- A client is a unique task generator.
- Server creates a workerthread for each connecting client.
- Server establishes a unique connection between client an workerthread.(Some tunnle)
- -> (REQ/REQ between each client-workerthread pair over one socket)
- Server must forward incoming tasks instantly.
- Server must be able to handle multiple clients (and therefore connections to WTs).
Hence its not possible to achieve the REQ/REP/REQ/REP/... sequence.
回答1:
Have a look at the several patterns described in the ZMQ Guide and see if one of them fits better than simple REQ/REP
.
In general your server on the linux box seems to need implementation of ROUTER/DEALER
pattern that will forward incoming tasks to a set of workers.
ZMQ has it's own topology compared to traditional socket based client/server solutions. Other solutions might be to implement a socket based server that uses a thread pool to forward the incoming tasks.
UPDATE: Since you've been precising on your requirements I think the majordomo pattern might be a good and reliable starting point for your purposes. There might be better fitting patterns as @gvd mentions in his comments.
回答2:
As DevNoob says, the asynchronous client server pattern sounds like the one you need.
"LRU" means least-recently used, but I've removed that term from the latest text of the Guide, and am using "load-balancing" instead. Since the goal is to load-balance across a set of workers...
回答3:
I know you mentioned ZMQ as your lib of choice, but may I humbly suggest Akka (www.akka.io)? Akka has a ZQM extension which we've had good experiences with, so you can readily translate between its transport mechanism (netty-based) and ZMQ. It implements the Actor Model and, more importantly, provides an 'ask' and a 'tell' for async processing across worker actors. Akka also seems to compete quite favorably with the likes of ZMQ in terms of speed of messaging and is documented far better in my experience.
One thing Akka does not have is cross-language support, but we're using the ZMQ extension to achieve this now. The combination of ZMQ and Akka has proven to be a very powerful and flexible platform indeed.
来源:https://stackoverflow.com/questions/13258030/asynchronous-tasks-which-architecture-or-pattern-in-zeromq