Created
April 11, 2012 16:38
-
-
Save lebedov/2360424 to your computer and use it in GitHub Desktop.
Demo of how to extend multiprocessing.Process to communicate with pyzmq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Demo of how to extend multiprocessing.Process to communicate with | |
pyzmq. | |
""" | |
import zmq | |
from multiprocessing import Process | |
import time | |
class ServerProcess(Process): | |
def __init__(self, ip, port): | |
Process.__init__(self) | |
self.ip = ip | |
self.port = port | |
def run(self): | |
self.address = 'tcp://%s:%s' % (self.ip, self.port) | |
self.context = zmq.Context() | |
self.socket = self.context.socket(zmq.REP) | |
self.socket.bind(self.address) | |
while True: | |
msg = self.socket.recv() | |
print 'server: ' + msg | |
self.socket.send(msg) | |
if msg == 'q': | |
break | |
class ClientProcess(Process): | |
def __init__(self, ip, port): | |
Process.__init__(self) | |
self.ip = ip | |
self.port = port | |
def run(self): | |
self.address = 'tcp://%s:%s' % (self.ip, self.port) | |
self.context = zmq.Context() | |
self.socket = self.context.socket(zmq.REQ) | |
self.socket.connect(self.address) | |
for i in range(5): | |
msg = 'msg %s' % i | |
print 'client: ' + msg | |
self.socket.send(msg) | |
reply = self.socket.recv() | |
print 'client: ack' | |
self.socket.send('q') | |
reply = self.socket.recv() | |
if __name__ == '__main__': | |
server = ServerProcess('*', 65000) | |
client = ClientProcess('localhost', 65000) | |
server.start() | |
time.sleep(3) | |
client.start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment