为什么从我的程序中删除多处理会在 math.sin() 函数中引发 "A float is needed" 错误?

Why does removing multiprocessing from my program throws an "A float is needed" error in math.sin() function?

我之前已经通过多处理实现了我的程序,但现在我正在使用 Numba,所以我想删除多处理。但是,我遇到了一些问题。 程序的一部分在这里给出,我使用的是spyder 3.2.8版本,它使用的是python 2.7。

theta = 0
sin_theta = math.sin(math.radians(theta))

多处理实现如下

dataset = []  
for i in range(0, 375, 15):
    for j in range(0, 195, 15):
        for k in range(0, 375, 15):
            dataset.append([i, j, k])


agents = 40
chunksize = 4
pool = mp.Pool(processes = agents)
result = pool.map(calling, dataset, chunksize)

去除多处理后如下

import os
dataset = []  
for i in range(0, 375, 15):
    for j in range(0, 195, 15):
        for k in range(0, 375, 15):
            dataset.append([i, j, k])


calling(dataset)

调用函数是

def calling(dataset):
    l1 = dataset[0]
    l2 = dataset[1]
    l3 = dataset[2]


    import random
    i = random.sample(range(9000), 1)


    t = 0
    f = 0
    z = 0


    global g_r
    global g_o


    g_o_r = grid_one
    global s_a_r, p_l
    rt(p_l, l1, l2, l3, i)

rt函数是

def rt(p, f, t, z, i):
    import math
    import cmath
    sin_t = math.sin(math.radians(t))
    sin_f = math.sin(math.radians(f))
    sin_z = math.sin(math.radians(z))
    cos_t = math.cos(math.radians(t))
    cos_f = math.cos(math.radians(f))
    cos_z = math.cos(math.radians(z))

错误是

sin_t = math.sin(math.radians(t))
TypeError: a float is required

请告知是否需要任何进一步的信息或数据。

您的 dataset 是一个列表列表,每个列表包含三个元素。当使用多处理并通过 pool.map 传递您的 dataset 时,外部列表被迭代并且三元素内部列表被传递给 calling.

因此,当您这样做时:

l1 = dataset[0]
l2 = dataset[1]
l3 = dataset[2]

l1、l2 和 l3 每个包含内部列表的三个元素之一。

现在您直接将 dataset 传递给 calling,l1、l2 和 l3 保存了您的 dataset 的前三个内部列表,所以基本上您传递了列表 [0, 0, 0][0, 0, 15][0, 0, 30] 下降到 math.radians(),因此出现错误。

要解决这个问题,只需像这样调用 calling

for inner in dataset:
    calling(inner)