Created
May 27, 2012 20:23
-
-
Save jamesdear/2815757 to your computer and use it in GitHub Desktop.
Publish 100 messages with pika twisted vs pika native
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 pika | |
import time | |
pika.log.setup(color=True) | |
connection = None | |
channel = None | |
# Import all adapters for easier experimentation | |
from pika.adapters import * | |
def on_connected(connection): | |
pika.log.info("demo_send: Connected to RabbitMQ") | |
connection.channel(on_channel_open) | |
def on_channel_open(channel_): | |
global channel | |
channel = channel_ | |
pika.log.info("demo_send: Received our Channel") | |
channel.queue_declare(queue="test", durable=True, | |
exclusive=False, auto_delete=False, | |
callback=on_queue_declared) | |
def on_queue_declared(frame): | |
pika.log.info("demo_send: Queue Declared") | |
for x in xrange(0, 10): | |
# import time; time.sleep(10) | |
message = "Hello World #%i: %.8f" % (x, time.time()) | |
pika.log.info("Sending: %s" % message) | |
channel.basic_publish(exchange='', | |
routing_key="flibble", | |
body=message, | |
properties=pika.BasicProperties( | |
content_type="text/plain", | |
delivery_mode=1)) | |
# Close our connection | |
connection.close() | |
if __name__ == '__main__': | |
host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1' | |
parameters = pika.ConnectionParameters(host) | |
connection = SelectConnection(parameters, on_connected) | |
try: | |
connection.ioloop.start() | |
except KeyboardInterrupt: | |
connection.close() | |
connection.ioloop.start() |
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 | |
from pika import log as pika_log | |
from pika.adapters.twisted_connection import TwistedConnection | |
from pika.adapters.twisted_connection import TwistedProtocolConnection | |
from pika.connection import ConnectionParameters | |
from pika import BasicProperties | |
from twisted.internet import task, protocol, reactor, defer | |
from twisted.python import log as twisted_log | |
class TwistedHandler(object): | |
def on_connected(self, connection): | |
pika_log.info("demo_twisted: Connected to RabbitMQ") | |
d = connection.channel() | |
d.addCallback(self.got_channel) | |
return d | |
def got_channel(self, channel): | |
pika_log.info("demo_twisted: Got the channel") | |
self.channel = channel | |
dl = defer.DeferredList([self.publish() for n in range(100)]) | |
dl.addCallback(self.after_publish) | |
return dl | |
def publish(self, *args, **kwargs): | |
properties=BasicProperties(delivery_mode=1) | |
pika_log.info("sending message") | |
d = self.channel.basic_publish(exchange="twisted", | |
routing_key='flibble', #routing_key, | |
body = "hello world", | |
properties=properties) | |
d.addErrback(lambda err: pika_log.info(str(err))) | |
return d | |
def after_publish(self, dl): | |
print "Sent %u msgs, %u Ok." % (len(dl), sum([1 for x in dl if x[0]])) | |
print dl | |
#reactor.stop() | |
if __name__ == '__main__': | |
handler = TwistedHandler() | |
parameters = ConnectionParameters() | |
pika_log.setup(color=True) | |
cc = protocol.ClientCreator(reactor, | |
TwistedProtocolConnection, parameters) | |
d = cc.connectTCP('localhost', 5672) | |
d.addCallback(lambda protocol: protocol.ready) | |
d.addCallback(handler.on_connected) | |
d.addErrback(twisted_log.err) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/jamesdear/2815757#file-pika_native_publish-py-L30 --> xrange(0, 10) vs xrange(0, 100)
Thanks for posting these examples.