簡體   English   中英

多處理python。 錯誤''dict'對象不可調用'

[英]Multiprocessing python. Error ''dict' object is not callable'

我正在嘗試在我的課程中使用多進程運行方法。 該方法返回一個字典。 我不明白為什么會收到錯誤“ TypeError:'dict'對象不可調用”。 我見過很多線程說實例方法不能被腌制,需要被手工腌制/去腌制。 但是,我在這里沒有得到這個錯誤,我想知道為什么。 我只想對一個實例方法進行多處理,然后在另一個實例方法中使用結果。 附件是最小的工作示例。

            #/usr/bin/python
            from multiprocessing import Pool

            class test:

                def __init__(self):
                    print "init"

                def  run(self):
                    y = {'write_bandwidth': 3768.3135113756257}
                    return y

                def pool(self):
                    pool = Pool(processes=2)              
                    result = pool.map(self.run(), range(10)) 
                    print result

            if __name__ == '__main__':
                t = test();
                t.pool()

這是我遇到的錯誤。

            init
            Traceback (most recent call last):
              File "/Users/preetigupta25/Documents/lab work/preeti_repos/preeti_repo/MSST_2015/economic-model/src/test3.py", line 20, in <module>
                t.pool()
              File "/Users/preetigupta25/Documents/lab work/preeti_repos/preeti_repo/MSST_2015/economic-model/src/test3.py", line 15, in pool
                result = pool.map(self.run(), range(10)) 
              File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 250, in map
                return self.map_async(func, iterable, chunksize).get()
              File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 554, in get
                raise self._value
            TypeError: 'dict' object is not callable
            [Finished in 0.2s with exit code 1]

函數簽名:

 def map(self, func, iterable, chunksize=None):

這是您的一個示例:

# coding=utf8
from multiprocessing import Pool


def f(x):
    return x * x


def run(self):
    y = {'write_bandwidth': 3768.3135113756257}
    return y


if __name__ == '__main__':
    p = Pool(5)
    print(p.map(run, range(3)))

結果:

[{'write_bandwidth': 3768.3135113756257}, {'write_bandwidth': 3768.3135113756257}, {'write_bandwidth': 3768.3135113756257}]

如果您還不了解,請單擊 https://docs.python.org/2/library/multiprocessing.html#introduction

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM