agoragames/haigha — Issue #9: haigha/gevent example
Author: vitaly-krugl
Created: 2012-03-08 04:51:19+00:00
State: open

I modified your "hello-world" example to work with gevent. In case you need a gevent example to include with the package, the following worked for me:

import gevent
from haigha.connection import Connection
from haigha.message import Message

connection = Connection(
user='guest', password='guest',
vhost='/', host='localhost',
heartbeat=None, debug=True, transport="gevent")

def consumer(msg):
print "CONSUMER: MESSAGE RECEIVED: ", msg
gevent.kill(g)
#NOTE: haigha's hello-world example had connection.close()
# here (instead of our geven.kill(g) statement). The close()
# resulted in deadlock when running with gevent as we're here.
# Even Ctrl-c wouldn't budge this deadlock!

ch = connection.channel()

ch.exchange.declare('test_exchange', 'direct', auto_delete=True)

ch.queue.declare('test_queue', auto_delete=True)

ch.queue.bind('test_queue', 'test_exchange', 'test_key')

ch.basic.consume('test_queue', consumer)

ch.basic.publish( Message('body', application_headers={'hello':'world'}),
'test_exchange', 'test_key' )

def readFrames(conn):
while True:
conn.read_frames()

g = gevent.spawn(readFrames, conn=connection)

result = g.get()
print "readFrames result: ", repr(result)

connection.close()