one zmq context per process

Former-commit-id: caa97e1fac0be7dc4d5a6a2e3ef81c2e3e3c6bd2
This commit is contained in:
Michael T. Kelbaugh
2020-02-27 10:24:38 -05:00
parent 7f5ddf11b9
commit 58ebb0b04e
2 changed files with 14 additions and 20 deletions

View File

@@ -87,14 +87,13 @@ class Broker:
message['current_year'] = self.incstep + self.initial_year
self.pub_queue.put(message)
def pub(self, event):
def pub(self, event, context):
"""
publishes messages to the models, via the forwarder's SUB
:param event: the shutdown event for managing threads
:return: runs continuously until the shutdown event is set, then closes its zmq socket
"""
context = zmq.Context()
sock = context.socket(zmq.PUB)
sock.setsockopt(zmq.LINGER, 1000)
sock.connect('tcp://broker:5555')
@@ -107,16 +106,14 @@ class Broker:
sock.send_json(message)
sock.close()
context.term()
def sub(self, event):
def sub(self, event, context):
"""
receives messages from the models, via the forwarder's PUB
:param event: the shutdown event for managing threads
:return: runs continuously until the shutdown event is set, then closes its zmq socket
"""
context = zmq.Context()
sock = context.socket(zmq.SUB)
sock.setsockopt(zmq.SUBSCRIBE, b"")
sock.setsockopt(zmq.RCVTIMEO, 0)
@@ -138,9 +135,8 @@ class Broker:
self.mongo_queue.put(('sub', message))
sock.close()
context.term()
def forwarder(self, event):
def forwarder(self, event, context):
"""
acts as a proxy between models by pushing messages received by the broker's SUB to the broker's PUB
:param event: the shutdown event for managing threads
@@ -148,7 +144,6 @@ class Broker:
"""
logging.info("started forwarder")
context = zmq.Context()
frontend = context.socket(zmq.SUB)
frontend.setsockopt(zmq.SUBSCRIBE, b"")
@@ -173,7 +168,6 @@ class Broker:
logging.critical("forwarder is shutting down")
frontend.close()
backend.close()
context.term()
def watchdog(self, event):
"""
@@ -254,14 +248,15 @@ class Broker:
"""
shutdown = Event()
context = zmq.Context()
forwarder_thread = Thread(target=self.forwarder, args=(shutdown,))
forwarder_thread = Thread(target=self.forwarder, args=(shutdown, context,))
forwarder_thread.start()
subscribe_thread = Thread(target=self.sub, args=(shutdown,))
subscribe_thread = Thread(target=self.sub, args=(shutdown, context,))
subscribe_thread.start()
publish_thread = Thread(target=self.pub, args=(shutdown,))
publish_thread = Thread(target=self.pub, args=(shutdown, context,))
publish_thread.start()
status_thread = Thread(target=self.send_status, args=(shutdown,))
@@ -286,6 +281,7 @@ class Broker:
except Exception as e:
logging.critical(e)
finally:
context.term()
shutdown.set()
logging.critical("broker has shut down")

View File

@@ -486,7 +486,7 @@ class OuterWrapper(ABC):
logging.debug(json.dumps(message))
def pub(self, event):
def pub(self, event, context):
"""
publishes messages to the broker, including status messages and data messages
Sets the shutdown event if an outgoing data message matches more than one output schema.
@@ -495,7 +495,6 @@ class OuterWrapper(ABC):
"""
# connect to zmq
context = zmq.Context()
sock = context.socket(zmq.PUB)
sock.setsockopt(zmq.LINGER, 1000)
sock.connect('tcp://broker:5555')
@@ -577,9 +576,8 @@ class OuterWrapper(ABC):
event.set()
sock.close()
context.term()
def sub(self, event):
def sub(self, event, context):
"""
connects to the broker's PUB as a subscriber and receives all messages sent from the broker,
and all messages sent by other models and forwarded by the broker.
@@ -589,7 +587,6 @@ class OuterWrapper(ABC):
"""
# connect to zmq
context = zmq.Context()
sock = context.socket(zmq.SUB)
sock.setsockopt(zmq.SUBSCRIBE, b"")
sock.setsockopt(zmq.RCVTIMEO, 0)
@@ -613,7 +610,6 @@ class OuterWrapper(ABC):
self.action_queue.put(message)
sock.close()
context.term()
def insert_data_message(self, message):
"""
@@ -743,13 +739,14 @@ class OuterWrapper(ABC):
# start the threads
shutdown = Event()
context = zmq.Context()
# listen for messages
subscribe_thread = Thread(target=self.sub, args=(shutdown,))
subscribe_thread = Thread(target=self.sub, args=(shutdown, context,))
subscribe_thread.start()
# publish messages
publish_thread = Thread(target=self.pub, args=(shutdown,))
publish_thread = Thread(target=self.pub, args=(shutdown, context,))
publish_thread.start()
# handle increments
@@ -770,5 +767,6 @@ class OuterWrapper(ABC):
except Exception as e:
logging.critical(e)
finally:
context.term()
shutdown.set()
logging.critical(f"{self.model_id} model has shut down")