简体   繁体   English

Eventlet 和 Python 守护进程,Foo 没有被调用?

[英]Eventlet and Python daemon, Foo not called?

I am trying to build a Python deamon which listen to a queue (Redis Kombu).我正在尝试构建一个监听队列(Redis Kombu)的 Python 守护进程。 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.我可以毫无问题地接收任务并使用它,但是当我尝试使用 eventlet 生成 GreenThread 时,它似乎根本没有做任何事情。

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.我需要调用 eventlet.monkey_patch() 来调用 sleep() 来触发上下文切换。

You just need to use eventlet.sleep as described here:您只需要使用eventlet.sleep ,如下所述:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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