Skip to content
Snippets Groups Projects
Commit 01b02a09 authored by Bo-Chun Chen's avatar Bo-Chun Chen
Browse files

Update RCRMQ class

publish funtion:
 only make connection if it is not yet established
 only disconnect if there's no consumer using the same connection

consume function:
 only make connection if it is not yet established
 adding _consuming variable to keep tracking if the connection needs to be alive
parent abc0fbfa
No related branches found
No related tags found
2 merge requests!23Feat resolve uid gid,!18Redesign RabbitMQ
......@@ -82,13 +82,15 @@ class RCRMQ(object):
if 'routing_key' in obj:
self.ROUTING_KEY = obj['routing_key']
self.connect()
if self._connection is None:
self.connect()
self._channel.basic_publish(exchange=self.EXCHANGE,
routing_key=self.ROUTING_KEY,
body=json.dumps(obj['msg']))
self.disconnect()
if not self._consuming:
self.disconnect()
def start_consume(self, obj):
if 'queue' in obj:
......@@ -100,9 +102,11 @@ class RCRMQ(object):
if self.DEBUG:
print("Queue: " + self.QUEUE + "\nRouting_key: " + self.ROUTING_KEY)
self.connect()
if self._connection is None:
self.connect()
self._consumer_tag = self._channel.basic_consume(self.QUEUE,obj['cb'])
self._consuming = True
try:
self._channel.start_consuming()
except KeyboardInterrupt:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment