使用greenlet同时执行多个函数的问题

发布于 2024-11-19 15:17:15 字数 1082 浏览 6 评论 0原文

以下脚本的目的是同时执行许多函数,但我不知道为什么它不能正常工作。
这些函数以顺序方式执行,而不是并行执行。

如果有任何建议可以澄清我做错了什么,我将不胜感激。

import time
import eventlet


EXECUTION_TIMEOUT = 10

def example(name, steps_limit):
    print 'Starting process %s with %d steps' % (name, steps_limit)
    for i in range(1, steps_limit+1):
        print "Process %s, step %d" % (name, i)
        time.sleep(2)
    print 'Finishing process %s with %d steps' % (name, steps_limit)


def fetch(input_data):
    example(input_data['name'], input_data['steps'])

test_data = [{'name':'proceso1', 'steps':3},
             {'name':'proceso2', 'steps':5},
             {'name':'proceso3', 'steps':6},
             ]

def main():
    #Setting up time out
    timeout = eventlet.timeout.Timeout(EXECUTION_TIMEOUT)
    #initialize pool
    pool = eventlet.GreenPool(size=1000)
    try:
        for hits in pool.imap(fetch, test_data):
            pass
    except eventlet.Timeout:
        print 'Operation interrupted by timeout concept'
    finally:
        timeout.cancel()


if __name__ == '__main__':
    main()

The following script has the purpose of execute many functions simultaneously, but i don't have idea why it's not working correctly.
The functions are executed in sequential way, not parallel.

I would appreciate any suggestion to clarify me, about what i'm doing wrong.

import time
import eventlet


EXECUTION_TIMEOUT = 10

def example(name, steps_limit):
    print 'Starting process %s with %d steps' % (name, steps_limit)
    for i in range(1, steps_limit+1):
        print "Process %s, step %d" % (name, i)
        time.sleep(2)
    print 'Finishing process %s with %d steps' % (name, steps_limit)


def fetch(input_data):
    example(input_data['name'], input_data['steps'])

test_data = [{'name':'proceso1', 'steps':3},
             {'name':'proceso2', 'steps':5},
             {'name':'proceso3', 'steps':6},
             ]

def main():
    #Setting up time out
    timeout = eventlet.timeout.Timeout(EXECUTION_TIMEOUT)
    #initialize pool
    pool = eventlet.GreenPool(size=1000)
    try:
        for hits in pool.imap(fetch, test_data):
            pass
    except eventlet.Timeout:
        print 'Operation interrupted by timeout concept'
    finally:
        timeout.cancel()


if __name__ == '__main__':
    main()

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

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

发布评论

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

评论(1

隐诗 2024-11-26 15:17:15

你说 time.sleep() 是罪魁祸首是对的。

但您不必将 time.sleep() 替换为 eventlet.sleep()。相反,您可以猴子补丁 time.sleep 成为 eventlet.sleep

事实上,这是首选,因为这样您不知道使用 time.sleep 的库也会得到修复,而如果您自己修复它,则只需修复您的代码。

因此,在 gevent 中,我们只建议调用 gevent.monkey.patch_all()对于所有程序。在eventlet中,也有类似的函数。

You're right about time.sleep() being the culprit.

But you don't have to replace time.sleep() with eventlet.sleep(). Instead, you can monkey patch time.sleep to become eventlet.sleep.

This is, in fact, preferred, because that way the libraries that you did not know use time.sleep also get fixed whereas if you fix it yourself you only fixed your code.

For this reason, in gevent, we just recommend to call gevent.monkey.patch_all() for all programs. In eventlet, there's a similar function exists.

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