本文介紹了與多處理錯誤的另一個混淆,“模塊"對象沒有屬性“f"的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我知道這個問題之前已經回答過了,但似乎直接執行腳本python filename.py"是行不通的.我在 SuSE Linux 上安裝了 Python 2.6.2.
I know this has been answered before, but it seems that executing the script directly "python filename.py" does not work. I have Python 2.6.2 on SuSE Linux.
代碼:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from multiprocessing import Pool
p = Pool(1)
def f(x):
return x*x
p.map(f, [1, 2, 3])
命令行:
> python example.py
Process PoolWorker-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/multiprocessing/process.py", line 231, in _bootstrap
self.run()
File "/usr/lib/python2.6/multiprocessing/process.py", line 88, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python2.6/multiprocessing/pool.py", line 57, in worker
task = get()
File "/usr/lib/python2.6/multiprocessing/queues.py", line 339, in get
return recv()
AttributeError: 'module' object has no attribute 'f'
推薦答案
重構代碼,以便在創建 Pool 實例之前定義 f()
函數.否則worker看不到你的函數.
Restructure your code so that the f()
function is defined before you create instance of Pool. Otherwise the worker cannot see your function.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from multiprocessing import Pool
def f(x):
return x*x
p = Pool(1)
p.map(f, [1, 2, 3])
這篇關于與多處理錯誤的另一個混淆,“模塊"對象沒有屬性“f"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!