Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RabbitmqConnectionFactory:
- # ...
- def __enter__(self):
- credentials = pika.credentials.PlainCredentials(
- self.username, self.password
- )
- self.connection = pika.BlockingConnection(
- pika.ConnectionParameters(self.host, self.port, "/", credentials)
- )
- self._make_new_channel()
- return self
- def __exit__(self, exc_type, exc_value, traceback):
- self._close_channel()
- self._close_connection()
- def _make_new_channel(self):
- if self.connection and self.connection.is_open:
- self.channel = self.connection.channel()
- def _close_channel(self):
- if self.channel and not self.channel.is_closed:
- self.channel.close()
- def _close_connection(self):
- if self.connection and not self.connection.is_closed:
- self.connection.close()
- def _get_channel(self):
- if (not self.connection or self.connection.is_closed or
- not self.channel or self.channel.is_closed):
- self.__exit__(None, None, None)
- self.__enter__()
- return self.channel
- # ... rest of the class
- # USE like this ONLY:
- with RabbitmqConnectionFactory(...) as rq:
- rq.declare_queue(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement