Python 多处理,TypeError
Python Multiprocessing, TypeError
我是多处理的新手,我需要你的帮助。
我有四个变量,每个变量最多可以有 4 个值(整数或浮点数),我将它们全部存储在一个名为 par=[A, B, C, D]
的列表中。 (见下文)
我已经用 par = itertools.product(*par)
创建了一个可能的组合列表。
然后,我调用一个函数 func1
,它接受这些参数和更多参数并计算东西。根据 func1
的结果,我调用了另一个计算内容的函数,然后写入文件。
我想 运行 这些作为一个整体与 multiprocessing.Pool
并行 我想将 func1
和 func2
嵌入另一个函数,称为 func_run
,并将其映射到我在上面创建的列表 par
。
总而言之,我的代码如下所示:
#values that I will use for func1
r = np.logspace(np.log10(5),np.log10(300),300)
T = 200*r
#Parameters for the sim
A = [0.1, 0.05, 0.001, 0.005]
B = [0.005, 0.025, 0.05, 0.1]
C = [20, 60, 100, 200]
D = [10, 20, 40, 80]
#Store them in a list
par = [A, B, C, D]
#Create a list with all combinations
par = list(itertools.product(*par))
def func_run(param):
for i in range(len(param)):
# Call func1
values = func1(param[i][0],param[i][1],param[i][2], param[i][3], r, T)
x = values[0]
y = values[1]
# and so on
# Call func2
results = func2(x,y,...)
z = results[0]
w = results[1]
# and so on
data_dict = {'result 1': [param[i][0]], 'result 2' : [param[i][1]]}
df = pd.DataFrame(data=data_dict)
with open(filename, 'a') as f:
df.to_csv(f, header=False)
return
然后,我用 multiprocessing
调用 func_run
。
from multiprocessing import Pool
pool = Pool(processes=4)
results = pool.map(func_run, par)
结果,我得到一个,TypeError
带有回溯:
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/user/anaconda3/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/user/anaconda3/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "<ipython-input-14-5ce94acfd95e>", line 5, in run
values = calc_val(param[i][0],param[i][1],param[i][2], param[i][3], r, T)
TypeError: 'float' object is not subscriptable
"""
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-15-f45146f68f66> in <module>()
1 pool = Pool(processes=4)
----> 2 test = pool.map(run,par)
~/anaconda3/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
264 in a list that is returned.
265 '''
--> 266 return self._map_async(func, iterable, mapstar, chunksize).get()
267
268 def starmap(self, func, iterable, chunksize=None):
~/anaconda3/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
TypeError: 'float' object is not subscriptable
不幸的是,不可能添加整个函数以及它们在做什么,因为它们有数百行。我希望你能体会到这种感觉,虽然你不能真正地自己复制它。
是否有可能 运行 像这样的多处理或者我需要不同的方法?
如果有人能帮助我理解错误并使其成为 运行.
那就太好了
结果
par = list(itertools.product(*par))
是浮点数(和整数)元组的列表。 Pool.map()
将 iterable 作为第二个参数并映射到它的项上,将它们单独传递给给定的 func。换句话说,函数 func_run(param)
param 不是数字元组列表,而是数字元组,因此
param[i][0]
正在尝试访问 ith float 对象的第 0 个项目,这当然没有意义,因此异常。您可能应该删除 func_run()
:
中的 for 循环
def func_run(param):
values = func1(param[0], param[1], param[2], param[3], r, T)
...
我是多处理的新手,我需要你的帮助。
我有四个变量,每个变量最多可以有 4 个值(整数或浮点数),我将它们全部存储在一个名为 par=[A, B, C, D]
的列表中。 (见下文)
我已经用 par = itertools.product(*par)
创建了一个可能的组合列表。
然后,我调用一个函数 func1
,它接受这些参数和更多参数并计算东西。根据 func1
的结果,我调用了另一个计算内容的函数,然后写入文件。
我想 运行 这些作为一个整体与 multiprocessing.Pool
并行 我想将 func1
和 func2
嵌入另一个函数,称为 func_run
,并将其映射到我在上面创建的列表 par
。
总而言之,我的代码如下所示:
#values that I will use for func1
r = np.logspace(np.log10(5),np.log10(300),300)
T = 200*r
#Parameters for the sim
A = [0.1, 0.05, 0.001, 0.005]
B = [0.005, 0.025, 0.05, 0.1]
C = [20, 60, 100, 200]
D = [10, 20, 40, 80]
#Store them in a list
par = [A, B, C, D]
#Create a list with all combinations
par = list(itertools.product(*par))
def func_run(param):
for i in range(len(param)):
# Call func1
values = func1(param[i][0],param[i][1],param[i][2], param[i][3], r, T)
x = values[0]
y = values[1]
# and so on
# Call func2
results = func2(x,y,...)
z = results[0]
w = results[1]
# and so on
data_dict = {'result 1': [param[i][0]], 'result 2' : [param[i][1]]}
df = pd.DataFrame(data=data_dict)
with open(filename, 'a') as f:
df.to_csv(f, header=False)
return
然后,我用 multiprocessing
调用 func_run
。
from multiprocessing import Pool
pool = Pool(processes=4)
results = pool.map(func_run, par)
结果,我得到一个,TypeError
带有回溯:
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/user/anaconda3/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/user/anaconda3/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "<ipython-input-14-5ce94acfd95e>", line 5, in run
values = calc_val(param[i][0],param[i][1],param[i][2], param[i][3], r, T)
TypeError: 'float' object is not subscriptable
"""
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-15-f45146f68f66> in <module>()
1 pool = Pool(processes=4)
----> 2 test = pool.map(run,par)
~/anaconda3/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
264 in a list that is returned.
265 '''
--> 266 return self._map_async(func, iterable, mapstar, chunksize).get()
267
268 def starmap(self, func, iterable, chunksize=None):
~/anaconda3/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
TypeError: 'float' object is not subscriptable
不幸的是,不可能添加整个函数以及它们在做什么,因为它们有数百行。我希望你能体会到这种感觉,虽然你不能真正地自己复制它。
是否有可能 运行 像这样的多处理或者我需要不同的方法? 如果有人能帮助我理解错误并使其成为 运行.
那就太好了结果
par = list(itertools.product(*par))
是浮点数(和整数)元组的列表。 Pool.map()
将 iterable 作为第二个参数并映射到它的项上,将它们单独传递给给定的 func。换句话说,函数 func_run(param)
param 不是数字元组列表,而是数字元组,因此
param[i][0]
正在尝试访问 ith float 对象的第 0 个项目,这当然没有意义,因此异常。您可能应该删除 func_run()
:
def func_run(param):
values = func1(param[0], param[1], param[2], param[3], r, T)
...