Created
May 19, 2017 02:29
-
-
Save subokita/f6dcfc9a3f0306ac192c8e22ffe6f774 to your computer and use it in GitHub Desktop.
Test pika
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import logging | |
from autologging import TRACE | |
logging.basicConfig( level = TRACE, | |
stream = sys.stdout, | |
format = "%(levelname)s:%(name)s:%(funcName)s:%(message)s" ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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