![](/img/trans.png)
[英]Question about using multiprocessing and slaves in Python. Getting error: <class 'TypeError'> 'bytes' object is not callable
[英]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.