pyzmq 使用
1. The Socket API
Creating and destroying sockets, which go together to form a karmic circle of socket life (see?zmq_socket,?zmq_close).Configuring sockets by setting options on them and checking them if necessary (see?zmq_setsockopt,?zmq_getsockopt).Plugging sockets onto the network topology by creating ?MQ connections to and from them (see?zmq_bind,?zmq_connect).Using the sockets to carry data by writing and receiving messages on them (see?zmq_send,?zmq_recv).2.Plugging Sockets Into the Topology
They go across an arbitrary transport (inproc,?ipc,?tcp,?pgm?or?epgm). See?zmq_inproc,?zmq_ipc,?zmq_tcp,?zmq_pgm, and?zmq_epgm.They exist when a client does?zmq_connect?to an endpoint, whether or not a server has already done?zmq_bind?to that endpoint.They are asynchronous, and have queues that magically exist where and when needed.They may express a certain "messaging pattern", according to the type of socket used at each end.One socket may have many outgoing and many incoming connections.There is no zmq_accept() method. When a socket is bound to an endpoint it automatically starts accepting connections.Your application code cannot work with these connections directly; they are encapsulated under the socket.A server node can bind to many endpoints and it can do this using a single socket. This means it will accept connections across different transports.?
4.Core Messaging Patterns
?
Request-reply, which connects a set of clients to a set of services. This is a remote procedure call and task distribution pattern.Publish-subscribe, which connects a set of publishers to a set of subscribers. This is a data distribution pattern.Pipeline, connects nodes in a fan-out / fan-in pattern that can have multiple steps, and loops. This is a parallel task distribution and collection pattern.We looked at each of these in the first chapter. There's one more pattern that people tend to try to use when they still think of ?MQ in terms of traditional TCP sockets:
Exclusive pair, which connects two sockets in an exclusive pair. This is a low-level pattern for specific, advanced use-cases. We'll see an example at the end of this chapter.These are the socket combinations that are valid for a connect-bind pair:PUB and SUBREQ and REPREQ and XREPXREQ and REPXREQ and XREPXREQ and XREQXREP and XREPPUSH and PULLPAIR and PAIR5.Working with Messages
Note than when you have passed a message to zmq_send, 0MQ will clear the message, i.e. set the size to zero. You cannot send the same message twice, and you cannot access the message data after sending it.
?
6.Handling Multiple Sockets?
In all the examples so far, the main loop of most examples has been:
1. wait for message on socket
2. process message
3. repeat
?
Let's start with a dirty hack, partly for the fun of not doing it right, but mainly because it lets me show you how to do non-blocking socket reads. Here is a simple example of reading from two sockets using non-blocking reads. This rather confused program acts both as a subscriber to weather updates, and a worker for parallel tasks:
?
?8. Handling Interrupt SignalsRealistic applications need to shutdown cleanly when interrupted with Ctrl-C or another signal such as SIGTERM. By default, these simply kill the process, meaning messages won't be flushed, files won't be closed cleanly, etc.
9. Detecting Memory Leaks?
10. Multipart Messages?
11. Intermediates and Devicesthen we extend the application across a wider network, placing devices in specific places and scaling up the number of nodes:
11.1 A Publish-Subscribe Proxy ServerWhen we extend request-reply, REQ talks to XREP and XREQ talks to REP. In between the XREQ and XREP we have to have code (like our broker) that pulls messages off the one socket and shoves them onto the other.
?
The request-reply broker binds to two endpoints, one for clients to connect to (the frontend socket) and one for services to connect to (the backend). To test this broker, you will want to change your services so they connect to the backend socket. Here are a client and service that show what I mean:
??
?
?
And here is the broker, in Python. You will see that it's multipart safe:
?
??
??
12. Multithreading with 0MQ
?
?
?
13. Signaling between Threads
In this example we use PAIR sockets over the?inproc?transport:
?
?
?
14. Node Coordination
?
?
?
?
17.?Making a (Semi-)Durable Subscriber
?
?
?
?
?
?
ps:
http://zguide.zeromq.org/page:all