Skip to content

Instantly share code, notes, and snippets.

@ask
Last active December 10, 2015 18:48
Show Gist options
  • Save ask/4476517 to your computer and use it in GitHub Desktop.
Save ask/4476517 to your computer and use it in GitHub Desktop.

Revisions

  1. ask revised this gist Jan 7, 2013. 1 changed file with 12 additions and 22 deletions.
    34 changes: 12 additions & 22 deletions a_rabbitmq_test.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #!/usr/bin/env python

    from signal import signal, SIGTERM, SIGINT
    import logging

    from pika import BlockingConnection, ConnectionParameters
    @@ -10,24 +9,15 @@
    logging.basicConfig(level=logging.INFO)

    # setting up rabbitmq pubsub
    rabbit_conn = BlockingConnection(ConnectionParameters('localhost'))
    rabbit_channel = rabbit_conn.channel()
    rabbit_channel.queue_declare(queue="my_queue")

    # closing everything on SIGTERM
    def handler(signum, frame):
    rabbit_channel.basic_cancel()
    rabbit_channel.close()
    rabbit_conn.close()
    logging.info("Stopped listening.")
    signal(SIGTERM, handler)
    signal(SIGINT, handler)

    def message_callback(ch, method, properties, body):
    logging.info("Got message '%s'" % body)

    rabbit_channel.basic_consume(message_callback, queue="my_queue", no_ack=True)

    # listen for items
    logging.info("Listen for items ...")
    rabbit_channel.start_consuming()
    with BlockingConnection(ConnectionParameters('localhost')) as rabbit_conn:
    with rabbit_conn.channel() as rabbit_chan:
    rabbit_channel.queue_declare(queue="my_queue")

    def message_callback(ch, method, properties, body):
    logging.info("Got message '%s'" % body)

    rabbit_channel.basic_consume(message_callback, queue="my_queue", no_ack=True)

    # listen for items
    logging.info("Listen for items ...")
    rabbit_channel.start_consuming()
  2. @dAnjou dAnjou revised this gist Jan 7, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions output
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ WARNING:pika.adapters.blocking_connection:Received Channel.Close, closing: <METH
    ERROR:pika.adapters.base_connection:Socket is closed
    WARNING:pika.adapters.blocking_connection:Received Channel.Close, closing: None
    Traceback (most recent call last):
    File "rabbitmq_test.py", line 33, in <module>
    File "a_rabbitmq_test.py", line 33, in <module>
    rabbit_channel.start_consuming()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 722, in start_consuming
    self.connection.process_data_events()
    @@ -19,7 +19,7 @@ Traceback (most recent call last):
    super(BlockingConnection, self)._handle_read()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/base_connection.py", line 296, in _handle_read
    data = self.socket.recv(self._buffer_size)
    File "rabbitmq_test.py", line 20, in handler
    File "a_rabbitmq_test.py", line 20, in handler
    rabbit_channel.close()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/channel.py", line 373, in close
    self._shutdown()
    @@ -51,4 +51,4 @@ Traceback (most recent call last):
    super(BlockingConnection, self)._send_frame(frame_value)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/connection.py", line 1312, in _send_frame
    raise exceptions.ConnectionClosed
    pika.exceptions.ConnectionClosed
    pika.exceptions.ConnectionClosed
  3. @dAnjou dAnjou revised this gist Jan 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion a_rabbitmq_test.py
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@
    rabbit_channel = rabbit_conn.channel()
    rabbit_channel.queue_declare(queue="my_queue")

    # closing writer on SIGTERM
    # closing everything on SIGTERM
    def handler(signum, frame):
    rabbit_channel.basic_cancel()
    rabbit_channel.close()
  4. @dAnjou dAnjou revised this gist Jan 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion output
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    $ python rabbitmq_test.py
    $ python a_rabbitmq_test.py
    INFO:pika.adapters.base_connection:Connecting fd 3 to localhost:5672
    INFO:pika.adapters.blocking_connection:Adapter connected
    INFO:root:Listen for items ...
  5. @dAnjou dAnjou revised this gist Jan 7, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  6. @dAnjou dAnjou created this gist Jan 7, 2013.
    54 changes: 54 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    $ python rabbitmq_test.py
    INFO:pika.adapters.base_connection:Connecting fd 3 to localhost:5672
    INFO:pika.adapters.blocking_connection:Adapter connected
    INFO:root:Listen for items ...
    ^CINFO:pika.channel:Channel.close(None, None)
    WARNING:pika.adapters.blocking_connection:Disconnected from RabbitMQ at localhost:5672 (504): CHANNEL_ERROR - expected 'channel.open'
    WARNING:pika.adapters.base_connection:Unknown state on disconnect: 0
    WARNING:pika.adapters.blocking_connection:Received Channel.Close, closing: <METHOD(['frame_type=1', 'channel_number=0', 'method=<Connection.Close([\'class_id=20\', \'method_id=40\', \'reply_code=504\', "reply_text=CHANNEL_ERROR - expected \'channel.open\'"])>'])>
    ERROR:pika.adapters.base_connection:Socket is closed
    WARNING:pika.adapters.blocking_connection:Received Channel.Close, closing: None
    Traceback (most recent call last):
    File "rabbitmq_test.py", line 33, in <module>
    rabbit_channel.start_consuming()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 722, in start_consuming
    self.connection.process_data_events()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 88, in process_data_events
    if self._handle_read():
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 184, in _handle_read
    super(BlockingConnection, self)._handle_read()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/base_connection.py", line 296, in _handle_read
    data = self.socket.recv(self._buffer_size)
    File "rabbitmq_test.py", line 20, in handler
    rabbit_channel.close()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/channel.py", line 373, in close
    self._shutdown()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 937, in _shutdown
    [spec.Channel.CloseOk])
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 904, in _rpc
    self.connection.process_data_events()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 88, in process_data_events
    if self._handle_read():
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 184, in _handle_read
    super(BlockingConnection, self)._handle_read()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/base_connection.py", line 300, in _handle_read
    return self._handle_error(error)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/base_connection.py", line 264, in _handle_error
    self._handle_disconnect()
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 181, in _handle_disconnect
    self._on_connection_closed(None, True)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 232, in _on_connection_closed
    self._channels[channel]._on_close(method_frame)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 817, in _on_close
    self._send_method(spec.Channel.CloseOk(), None, False)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 920, in _send_method
    self.connection.send_method(self.channel_number, method_frame, content)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 120, in send_method
    self._send_method(channel_number, method_frame, content)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/connection.py", line 1331, in _send_method
    self._send_frame(frame.Method(channel_number, method_frame))
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 245, in _send_frame
    super(BlockingConnection, self)._send_frame(frame_value)
    File "/home/max/.virtualenvs/IS/local/lib/python2.7/site-packages/pika/connection.py", line 1312, in _send_frame
    raise exceptions.ConnectionClosed
    pika.exceptions.ConnectionClosed
    33 changes: 33 additions & 0 deletions rabbitmq_test.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #!/usr/bin/env python

    from signal import signal, SIGTERM, SIGINT
    import logging

    from pika import BlockingConnection, ConnectionParameters


    if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    # setting up rabbitmq pubsub
    rabbit_conn = BlockingConnection(ConnectionParameters('localhost'))
    rabbit_channel = rabbit_conn.channel()
    rabbit_channel.queue_declare(queue="my_queue")

    # closing writer on SIGTERM
    def handler(signum, frame):
    rabbit_channel.basic_cancel()
    rabbit_channel.close()
    rabbit_conn.close()
    logging.info("Stopped listening.")
    signal(SIGTERM, handler)
    signal(SIGINT, handler)

    def message_callback(ch, method, properties, body):
    logging.info("Got message '%s'" % body)

    rabbit_channel.basic_consume(message_callback, queue="my_queue", no_ack=True)

    # listen for items
    logging.info("Listen for items ...")
    rabbit_channel.start_consuming()