Skip to content

Instantly share code, notes, and snippets.

@corntoole
Forked from anonymous/sampleforwarder.py
Created March 20, 2012 17:22
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 corntoole/2138347 to your computer and use it in GitHub Desktop.
Save corntoole/2138347 to your computer and use it in GitHub Desktop.
ZMQ Forwarder
import zmq
import signal
incoming = None
outgoing = None
context = None
# this method handles the termination of the app
# this handler is responsible for properly cleaning of the ports
def termSignalHandler(signum,frame):
global incoming, outgoing, context
incoming.close()
outgoing.close()
context.term()
print("termSignal Handler Called")
def startForwarder():
global incoming, outgoing, context
context = zmq.Context(1)
incoming = context.socket(zmq.SUB)
outgoing = context.socket(zmq.PUB)
try:
incoming.connect("tcp://127.0.0.1:5559");
incoming.setsockopt(zmq.SUBSCRIBE, "")
except:
print("incoming socket is already open")
try:
outgoing.bind('tcp://127.0.0.1:4449')
except:
print("outgoing socket is open")
zmq.device(zmq.FORWARDER, incoming, outgoing)
signal.signal(signal.SIGTERM,termSignalHandler)
startForwarder()
@corntoole
Copy link
Author

incoming, outgoing and context are visible within the termSignalHandler function, you need to add a global statement at the beginning of the function definition, like so: global incoming, outgoing, context

@abhinavsingh
Copy link

Shouldn't it be incoming.bind on line 24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment