問題描述
使用 pool.map(funct, iterable)
時出現此錯誤:
Am getting this error when using the pool.map(funct, iterable)
:
AttributeError: __exit__
沒有解釋,只是堆棧跟蹤到模塊內的 pool.py 文件.
No Explanation, only stack trace to the pool.py file within the module.
這樣使用:
with Pool(processes=2) as pool:
pool.map(myFunction, mylist)
pool.map(myfunction2, mylist2)
我懷疑picklability可能存在問題(python需要pickle
,或將列表數據轉換為字節流)但我不確定這是真的還是如何調試.
I suspect there could be a problem with the picklability (python needs to pickle
, or transform list data into byte stream) yet I'm not sure if this is true or if it is how to debug.
產生此錯誤的新代碼格式:
new format of code that produces this error :
def governingFunct(list):
#some tasks
def myFunction():
# function contents
with closing(Pool(processes=2)) as pool:
pool.map(myFunction, sublist)
pool.map(myFunction2, sublist2)
產生錯誤:
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
推薦答案
在 Python 2.x 和 3.0、3.1 和 3.2 中,multiprocessing.Pool()
對象不是上下文管理器.您不能在 with
語句中使用它們.只有在 Python 3.3 及更高版本中,您才能使用它們.來自 Python 3 multiprocessing.Pool()
文檔:
In Python 2.x and 3.0, 3.1 and 3.2, multiprocessing.Pool()
objects are not context managers. You cannot use them in a with
statement. Only in Python 3.3 and up can you use them as such. From the Python 3 multiprocessing.Pool()
documentation:
3.3 版中的新功能:池對象現在支持上下文管理協議 - 請參閱上下文管理器類型.__enter__()
返回池對象,__exit__()
調用 terminate().
New in version 3.3: Pool objects now support the context management protocol – see Context Manager Types.
__enter__()
returns the pool object, and__exit__()
calls terminate().
對于早期的 Python 版本,您可以使用 contextlib.closing()
,但考慮到這將調用 pool.close()
,而不是 pool.terminate()
.在這種情況下手動終止:
For earlier Python versions, you could use contextlib.closing()
, but take into account this'll call pool.close()
, not pool.terminate()
. Terminate manually in that case:
from contextlib import closing
with closing(Pool(processes=2)) as pool:
pool.map(myFunction, mylist)
pool.map(myfunction2, mylist2)
pool.terminate()
或創建自己的 terminating()
上下文管理器:
or create your own terminating()
context manager:
from contextlib import contextmanager
@contextmanager
def terminating(thing):
try:
yield thing
finally:
thing.terminate()
with terminating(Pool(processes=2)) as pool:
pool.map(myFunction, mylist)
pool.map(myfunction2, mylist2)
這篇關于Python 多處理庫錯誤(AttributeError:__exit__)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!