mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-06 00:01:20 +00:00
26 lines
1000 B
Python
26 lines
1000 B
Python
|
from functools import partial
|
||
|
|
||
|
import zmq
|
||
|
from zmq.eventloop import ioloop
|
||
|
from zmq.eventloop.zmqstream import ZMQStream
|
||
|
|
||
|
from config import PUBLISHER_PORT, RECEIVER_PORT
|
||
|
|
||
|
|
||
|
class Messaging:
|
||
|
def __init__(self):
|
||
|
ioloop.install()
|
||
|
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])
|