简体   繁体   中英

Eventlet and Python daemon, Foo not called?

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.

You just need to use eventlet.sleep as described here:

http://eventlet.net/doc/basic_usage.html#eventlet.sleep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM