I am trying to build a Python deamon which listen to a queue (Redis Kombu). Grab the task and spawn a greenthread to process this task.
I can receive the task and consume it without trouble but when I try to spawn a GreenThread with eventlet it does not seem to be doing anything at all.
No print, no logging is shown.
class agent(Daemon):
"""
Agent
"""
def run(self):
# Setup connection
mainLogger.debug('Connecting to Redis')
connection = BrokerConnection(
hostname=agentConfig['redis_host'],
transport="redis",
virtual_host=agentConfig['redis_db'],
port=int(agentConfig['redis_port']))
connection.connect()
# Create an eventlet pool of size 5
pool = eventlet.GreenPool(5)
q = connection.SimpleQueue("myq")
while True:
try:
message = q.get(block=True, timeout=1)
print "GOT A MESSAGE FROM Q !"
pool.spawn_n(self.foo, 'x')
print "END SPAWN !"
except Empty:
mainLogger.debug('No tasks, going to sleep')
time.sleep(1)
def foo(self, x):
mainLogger.debug('\o/')
print "HELLO FROM SPAWN"
Anything I am doing wrong ?
I needed to call eventlet.monkey_patch() for sleep() call to trigger context switching.