-
-
Save corntoole/2138347 to your computer and use it in GitHub Desktop.
ZMQ Forwarder
This file contains 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
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
incoming, outgoing and context
are visible within thetermSignalHandler
function, you need to add a global statement at the beginning of the function definition, like so:global incoming, outgoing, context