ZeroMQ export module, close the context

pull/941/head
Nicolargo 2016-10-17 11:10:35 +02:00
parent 1149608a97
commit d37c4306df
2 changed files with 10 additions and 5 deletions

View File

@ -322,7 +322,8 @@ db=glances
[zeromq]
# Configuration for the --export-zeromq option
# http://www.zeromq.org
host=127.0.0.1
# Use * to bind on all interfaces
host=*
port=5678
# Glances envelopes the stats in a publish message with two frames:
# - First frame containing the following prefix (STRING)

View File

@ -45,6 +45,7 @@ class Export(GlancesExport):
sys.exit(2)
# Init the ZeroMQ context
self.context = None
self.client = self.init()
def load_conf(self, section="zeromq"):
@ -74,8 +75,8 @@ class Export(GlancesExport):
server_uri = 'tcp://{}:{}'.format(self.host, self.port)
try:
context = zmq.Context()
publisher = context.socket(zmq.PUB)
self.context = zmq.Context()
publisher = self.context.socket(zmq.PUB)
publisher.bind(server_uri)
except Exception as e:
logger.critical("Cannot connect to ZeroMQ server %s (%s)" % (server_uri, e))
@ -86,8 +87,11 @@ class Export(GlancesExport):
return publisher
def exit(self):
"""Close the socket"""
self.client.close()
"""Close the socket and context"""
if self.client is not None:
self.client.close()
if self.context is not None:
self.context.destroy()
def export(self, name, columns, points):
"""Write the points to the ZeroMQ server."""