baseimage-tutorial-framework/src/event_handlers/messaging.py

27 lines
993 B
Python
Raw Normal View History

from functools import partial
import zmq
from zmq.eventloop import ioloop
from zmq.eventloop.zmqstream import ZMQStream
from config import PUBLISHER_PORT, RECEIVER_PORT
2018-01-25 14:24:01 +00:00
ioloop.install()
class Messaging:
def __init__(self):
self._zmq_context = zmq.Context.instance()
self._zmq_sub_socket = self._zmq_context.socket(zmq.SUB)
self._zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT))
self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket)
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
self._zmq_push_socket.connect('tcp://localhost:{}'.format(RECEIVER_PORT))
self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE)
self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE)
self.register_callback = self._zmq_sub_stream.on_recv
def send(self, anchor, response):
self._zmq_push_socket.send_multipart([anchor, response])