0% found this document useful (0 votes)
119 views2 pages

Cliente Servidor - ZMQ Request-Reply Server: Import

The document describes several ZeroMQ socket patterns for client-server and publish-subscribe messaging architectures. The request-reply pattern shows a server that uses a REP socket to receive and reply to client requests over TCP. The client uses a REQ socket to send requests and receive responses. The publish-subscribe pattern demonstrates a server with a PUB socket that publishes messages to subscribers. A client uses a SUB socket to subscribe to and receive messages from the publisher. The pipeline pattern shows sources that use PUSH sockets to distribute workloads to workers over TCP. Workers use PULL sockets to receive and process workloads from multiple sources.

Uploaded by

Romero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views2 pages

Cliente Servidor - ZMQ Request-Reply Server: Import

The document describes several ZeroMQ socket patterns for client-server and publish-subscribe messaging architectures. The request-reply pattern shows a server that uses a REP socket to receive and reply to client requests over TCP. The client uses a REQ socket to send requests and receive responses. The publish-subscribe pattern demonstrates a server with a PUB socket that publishes messages to subscribers. A client uses a SUB socket to subscribe to and receive messages from the publisher. The pipeline pattern shows sources that use PUSH sockets to distribute workloads to workers over TCP. Workers use PULL sockets to receive and process workloads from multiple sources.

Uploaded by

Romero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Cliente Servidor – ZMQ

Request-Reply Server
1 import zmq
2 context = zmq.Context()
3
4 p1 = "tcp://"+ HOST +":"+ PORT1 # how and where to connect
5 p2 = "tcp://"+ HOST +":"+ PORT2 # how and where to connect
6 s = context.socket(zmq.REP) # create reply socket
7
8 s.bind(p1) # bind socket to address
9 s.bind(p2) # bind socket to address
10 while True:
11 message = s.recv()# wait for incoming message
12 ifnot "STOP" in message: # if not to stop...
13 s.send(message + "*") # append "*" to message
14 else: # else...
15 break # break out of loop and end

Request-Reply Client
1 import zmq
2 context = zmq.Context()
3
4 php = "tcp://"+ HOST +":"+ PORT # how and where to connect
5 s = context.socket(zmq.REQ) # create socket
6
7 s.connect(php) # block until connected
8 s.send("Hello World") # send message
9 message = s.recv() # block until response
10 s.send("STOP") # tell server to stop
11 print message # print result

Publish-Subscribe Server
1 import zmq, time
2
3 context = zmq.Context()
4 s = context.socket(zmq.PUB) # create a publisher socket
5 p = "tcp://"+ HOST +":"+ PORT # how and where to communicate
6 s.bind(p) # bind socket to the address
7 while True:
8 time.sleep(5) # wait every 5 seconds
9 s.send("TIME " + time.asctime()) # publish the current time

Publish-Subscribe Client
1 import zmq
2
3 context = zmq.Context()
4 s = context.socket(zmq.SUB) # create a subscriber socket
5 p = "tcp://"+ HOST+":"+ PORT # how and where to communicate
6 s.connect(p) #connect to the server
7 s.setsockopt(zmq.SUBSCRIBE, "TIME") # subscribe to TIME messages
8
9 for i in range(5): # Five iterations
10 time = s.recv() # receive a message
11 print time

Pipeline Source
1 import zmq, time, pickle, sys, random
2
3 context = zmq.Context()
4 me = str(sys.argv[1])
5 s = context.socket(zmq.PUSH) # create a push socket
6 src = SRC1 if me == ’1’ else SRC2 # check task source host
7 prt = PORT1 if me == ’1’ else PORT2 # check task source port
8 p = "tcp://"+ src +":"+ prt # how and where to connect
9 s.bind(p) # bind socket to address
10
11 for i in range(100): # generate 100 workloads
12 workload = random.randint(1, 100) # compute workload
13 s.send(pickle.dumps((me,workload))) # send workload to worker

Pipeline Worker
1 import zmq, time, pickle, sys
2
3 context = zmq.Context()
4 me = str(sys.argv[1])
5 r = context.socket(zmq.PULL) # create a pull socket
6 p1 = "tcp://"+ SRC1+":"+ PORT1 # address first task source
7 p2 = "tcp://"+ SRC2+":"+ PORT2 # address second task source
8 r.connect(p1) # connect to task source 1
9 r.connect(p2) # connect to task source 2
10
11 while True:
12 work = pickle.loads(r.recv()) # receive work from a source
13 time.sleep(work[1]*0.01) # pretend to work

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy