码迷,mamicode.com
首页 > 其他好文 > 详细

ZeroMQ-Multiprocess

时间:2017-07-25 00:59:38      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:zeromq

# request_reply_processes.py
import zmq
import time
import sys
from  multiprocessing import Process

def server(port="5556"):
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.bind("tcp://*:%s" % port)
    print "Running server on port: ", port
    # serves only 5 request and dies
    for reqnum in range(5):
        # Wait for next request from client
        message = socket.recv()
        print "Received request #%s: %s" % (reqnum, message)
        socket.send("World from %s" % port)
         
def client(ports=["5556"]):

    context = zmq.Context()
    print "Connecting to server with ports %s" % ports
    socket = context.socket(zmq.REQ)
    for port in ports:
        socket.connect ("tcp://localhost:%s" % port)
    for request in range (20):
        print "Sending request ", request,"..."
        socket.send ("Hello")
        message = socket.recv()
        print "Received reply ", request, "[", message, "]"
        time.sleep (1)


if __name__ == "__main__":
    # Now we can run a few servers 
    server_ports = range(5550,5558,2)
    for server_port in server_ports:
        Process(target=server, args=(server_port,)).start()
        
    # Now we can connect a client to all these servers
    Process(target=client, args=(server_ports,)).start()
# running it:
(D:\anaconda) C:\Users\admin\Desktop\opt>python request_reply_processes.py

# result:
(D:\anaconda) C:\Users\admin\Desktop\opt>python request_reply_processes.py
Running server on port:  5550
Running server on port:  5552
Running server on port:  5554
Connecting to server with ports [5550, 5552, 5554, 5556]
Running server on port:  5556
Sending request  0 ...
Received request #0: Hello
Received reply  0 [ World from 5550 ]
Sending request  1 ...
Received request #0: Hello
Received reply  1 [ World from 5552 ]
Sending request  2 ...
Received request #0: Hello
Received reply  2 [ World from 5554 ]
Sending request  3 ...
Received request #0: Hello
Received reply  3 [ World from 5556 ]
Sending request  4 ...
Received request #1: Hello
Received reply  4 [ World from 5550 ]
Sending request  5 ...
Received request #1: Hello
Received reply  5 [ World from 5552 ]
Sending request  6 ...
Received request #1: Hello
Received reply  6 [ World from 5554 ]
Sending request  7 ...
Received request #1: Hello
Received reply  7 [ World from 5556 ]
Sending request  8 ...
Received request #2: Hello
Received reply  8 [ World from 5550 ]
Sending request  9 ...
Received request #2: Hello
Received reply  9 [ World from 5552 ]
Sending request  10 ...
Received request #2: Hello
Received reply  10 [ World from 5554 ]
Sending request  11 ...
Received request #2: Hello
Received reply  11 [ World from 5556 ]
Sending request  12 ...
Received request #3: Hello
Received reply  12 [ World from 5550 ]
Sending request  13 ...
Received request #3: Hello
Received reply  13 [ World from 5552 ]
Sending request  14 ...
Received request #3: Hello
Received reply  14 [ World from 5554 ]
Sending request  15 ...
Received request #3: Hello
Received reply  15 [ World from 5556 ]
Sending request  16 ...
Received request #4: Hello
Received reply  16 [ World from 5550 ]
Sending request  17 ...
Received request #4: Hello
Received reply  17 [ World from 5552 ]
Sending request  18 ...
Received request #4: Hello
Received reply  18 [ World from 5554 ]
Sending request  19 ...
Received request #4: Hello
Received reply  19 [ World from 5556 ]

(D:\anaconda) C:\Users\admin\Desktop\opt>


ZeroMQ-Multiprocess

标签:zeromq

原文地址:http://f1yinsky.blog.51cto.com/12568071/1950538

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!