使用greenlet同时执行多个函数的问题
以下脚本的目的是同时执行许多函数,但我不知道为什么它不能正常工作。
这些函数以顺序方式执行,而不是并行执行。
如果有任何建议可以澄清我做错了什么,我将不胜感激。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说
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()
witheventlet.sleep()
. Instead, you can monkey patchtime.sleep
to becomeeventlet.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.