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

发布于 2024-11-18 20:03:17 字数 1184 浏览 5 评论 0原文

我正在尝试构建一个监听队列(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"

我做错了什么吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2024-11-25 20:03:17

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

I needed to call eventlet.monkey_patch() for sleep() call to trigger context switching.

如梦 2024-11-25 20:03:17

您只需要按照此处所述使用 eventlet.sleep

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文