Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created April 11, 2012 16:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lebedov/2360424 to your computer and use it in GitHub Desktop.
Save lebedov/2360424 to your computer and use it in GitHub Desktop.
Demo of how to extend multiprocessing.Process to communicate with pyzmq
#!/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