繁体   English   中英

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

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

我正在尝试构建一个监听队列(Redis Kombu)的 Python 守护进程。 抓取任务并生成一个绿色线程来处理此任务。

我可以毫无问题地接收任务并使用它,但是当我尝试使用 eventlet 生成 GreenThread 时,它似乎根本没有做任何事情。

不打印,不显示日志记录。

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"

有什么我做错了吗?

我需要调用 eventlet.monkey_patch() 来调用 sleep() 来触发上下文切换。

您只需要使用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