简体   繁体   English

在for循环中使用事件池在python中进行第一次迭代后产生错误

[英]Using eventlet pool inside a for loop gives error after first iteration in python

When I run the code below, I get an error during second iteration. 当我运行下面的代码时,在第二次迭代中出现错误。
I wonder if it's because I didn't terminate the first pool before generating a new pool? 我想知道是否是因为在生成新池之前没有终止第一个池? If so, how do I terminate a pool? 如果是这样,我如何终止游泳池?
I get the same error when I define the pool outside the for loop. 当我在for循环外定义池时,出现相同的错误。

num_parallel_loop = 6;
collect_result = []
for i in range(n):
    pool = eventlet.GreenPool(size=num_parallel_loop)
    for result in pool.imap(func, dictionary.iteritems()):
         collect_result.append(result)

Error: 错误:

Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(self.__args, *self.__kwargs)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 325, in _handle_workers
while thread._state == RUN or (pool._cache and thread._state != TERMINATE):
AttributeError: '_MainThread' object has no attribute '_state'

I'm not familiar with eventlet, but my brief look at the documentation makes me wonder whether you have done the patching step. 我对eventlet不熟悉,但是对文档的简短浏览使我想知道您是否已完成补丁步骤。

One of the challenges of writing a library like Eventlet is that the built-in networking libraries don't natively support the sort of cooperative yielding that we need. 编写类似Eventlet的库的挑战之一是内置的网络库本身不支持我们需要的那种协作收益。 What we must do instead is patch standard library modules in certain key places so that they do cooperatively yield. 我们必须做的是在某些关键位置修补标准库模块,以便它们协同产生。 We've in the past considered doing this automatically upon importing Eventlet, but have decided against that course of action because it is un-Pythonic to change the behavior of module A simply by importing module B. 过去我们曾考虑过在导入Eventlet时自动执行此操作,但已决定不采取这种行动,因为仅通过导入模块B来更改模块A的行为就不符合Python规范。

Therefore, the application using Eventlet must explicitly green the world for itself, using one or both of the convenient methods provided. 因此,使用Eventlet的应用程序必须使用所提供的一种或两种便捷方法为自己明确地绿化世界。

 import eventlet httplib2 = eventlet.import_patched('httplib2') 

 import eventlet eventlet.monkey_patch() 

This bug has the same error you reported, and it seems to be a problem with the patching, so have you done the patching at all? 该错误与您报告的错误相同,并且似乎与修补程序有关,因此您是否完成了修补程序?

Update 更新

I tried a small example with eventlet, and I didn't see the problem you describe. 我用eventlet尝试了一个小例子,但没有看到您描述的问题。 Look at this example, and see if it's different from what you are doing. 查看此示例,看看它是否与您所做的不同。 If so, put a complete example in your question. 如果是这样,请在您的问题中提供完整的示例。

import eventlet

eventlet.monkey_patch()


def func(item):
    k, s = item
    return k * s

dictionary = {1: 'a',
              2: 'b',
              3: 'c',
              4: 'd',
              5: 'e',
              6: 'f',
              7: 'g',
              8: 'h'}
num_parallel_loop = 6
n = 3
collect_result = []
for i in range(n):
    pool = eventlet.GreenPool(size=num_parallel_loop)
    for result in pool.imap(func, dictionary.iteritems()):
        collect_result.append(result)

print(repr(collect_result))

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

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