Skip to content

Instantly share code, notes, and snippets.

@yjxiong
Forked from jpetazzo/README.md
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjxiong/7c446ca6bf9ae2536037 to your computer and use it in GitHub Desktop.
Save yjxiong/7c446ca6bf9ae2536037 to your computer and use it in GitHub Desktop.
Provides a hub for zerorpc worker-client pattern

Zerohub

This is used to have a bunch of ZeroRPC clients and workers talking to each other.

WARNING: this is not compatible with heartbeats and streaming!

Clients connect to the "in" side of the hub.

Workers connect to the "out" side of the hub.

That's all!

in: "tcp://*:4242"
out: "tcp://*:4244"
type: queue
#!/usr/bin/env python
import sys
import argparse
from collections import namedtuple
import zmq
import yaml
DeviceTypes = namedtuple('DeviceTypes', 'type input output')
device_map = {
'queue': DeviceTypes(zmq.QUEUE, zmq.XREP, zmq.XREQ),
'forwarder': DeviceTypes(zmq.FORWARDER, zmq.SUB, zmq.PUB),
'streamer': DeviceTypes(zmq.STREAMER, zmq.PULL, zmq.PUSH)
}
parser = argparse.ArgumentParser(description='Generic zeromq device')
parser.add_argument('config', type=file)
def main():
args = parser.parse_args()
config = yaml.load(args.config)
for k in ('in', 'out', 'type'):
if k not in config:
print >>sys.stderr, 'Configuration invalid. "{0}" is missing.'.format(k)
return 1
type_ = config['type']
if type_ not in device_map:
print >>sys.stderr, 'Incorrect device type: {0}'.format(type_)
return 1
context = zmq.Context()
device_type = device_map[type_]
input_ = context.socket(device_type.input)
input_.bind(config['in'])
if type_ == 'forwarder':
input_.setsockopt(zmq.SUBSCRIBE, '')
output = context.socket(device_type.output)
output.bind(config['out'])
zmq.device(device_type.type, input_, output)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment