Skip to content

Instantly share code, notes, and snippets.

@Barolina
Created May 7, 2020 07:10
Show Gist options
  • Save Barolina/df97aee8a4d506dbaa481df7935166b7 to your computer and use it in GitHub Desktop.
Save Barolina/df97aee8a4d506dbaa481df7935166b7 to your computer and use it in GitHub Desktop.
pythin and pika and recovery (while)
Blocking connection can be recovered in exception handling style (similar to php client). For example:

while True:
    connection = pika.BlockingConnection(parameters)

    channel = connection.channel()
    channel.basic_consume(on_message, 'queue')

    try:
        channel.start_consuming()
    except pika.exceptions.ConnectionClosed:
        LOGGER.info('Connection closed. Recovering')
        continue
    except KeyboardInterrupt:
        channel.stop_consuming()

    connection.close()
    break
    ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment