Skip to content

Instantly share code, notes, and snippets.

@subokita
Created May 19, 2017 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subokita/f6dcfc9a3f0306ac192c8e22ffe6f774 to your computer and use it in GitHub Desktop.
Save subokita/f6dcfc9a3f0306ac192c8e22ffe6f774 to your computer and use it in GitHub Desktop.
Test pika
import sys
import logging
from autologging import TRACE
logging.basicConfig( level = TRACE,
stream = sys.stdout,
format = "%(levelname)s:%(name)s:%(funcName)s:%(message)s" )
#!/usr/bin/env python
import autologging
import better_exceptions
import pika
@autologging.traced
class Publisher( object ):
def __init__( self ):
self._connection_params = pika.ConnectionParameters()
self._connection = pika.BlockingConnection( self._connection_params )
self._channel = self._connection.channel()
return
def publish( self ):
self._channel.basic_publish( exchange = '',
routing_key = 'test-queue',
body = 'test',
properties = pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
return
print("[START] Publisher")
publisher = Publisher()
publisher.publish()
#!/usr/bin/env python
import autologging
import better_exceptions
import pika
@autologging.traced
class Subscriber( object ):
def __init__( self ):
self._connection_params = pika.ConnectionParameters()
self._connection = pika.SelectConnection( self._connection_params, self.on_connected )
self._channel = None
return
def on_connected( self, connection ):
self._connection = connection
connection.channel( self.on_channel_open )
return
def on_channel_open( self, channel ):
self._channel = channel
channel.queue_declare( queue = "test-queue",
durable = True,
exclusive = False,
auto_delete = False,
callback = self.on_queue_declared )
return
def on_queue_declared( self, frame ):
self._channel.basic_consume( self.on_delivery, queue = 'test-queue' )
return
def on_delivery( self, channel, method, header, body ):
return
def run( self ):
try:
self._connection.ioloop.start()
except KeyboardInterrupt:
self._connection.close()
self._connection.ioloop.start()
return
print( "[START] Subscriber" )
subscriber = Subscriber()
subscriber.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment