当我使用多处理模块时,程序运行三次

时间:2016-01-01 05:04:10

标签: python-3.x parallel-processing multiprocessing

px = 1 + x ** 2
cx = x ** 0
fx = (-5 / 16) * (1 / x ** (3 / 4)) - (29 / 16) * x ** (5 / 4)
newmann_g0 = "none"
newmann_gl = 2.5
dirichlet_u0 = 0
dirichlet_ul = "none"

# Deciding if it is uniform or geometric and finding the Nod Point & Mesh
length = 0.1
num_element = int(1 / length)
num_nod_point = num_element + 1
degree = [3 for i in range(num_element)]
nod_point = [0]
for i in range(1, num_element + 1):
    nod_point += [i * length]
h = [length for _ in range(1, num_nod_point)]
max_degree = max(degree)

# Finding the Legendre Polynomial by iteration
legendre_poly = [1, x]
for n in range(1, max_degree):
    legendre_poly.append(x * legendre_poly[n] * (2 * n + 1) / (n + 1) - legendre_poly[n - 1] * n / (n + 1))

# Calculating the Shape Function by iteration
shape_function = [-0.5 * x + 0.5, 0.5 * x + 0.5] + [(legendre_poly[n - 1] - legendre_poly[n - 3]) * ((1 / (2 * (2 * n - 3))) ** 0.5) for n in range(3, max_degree + 2)]

# Calculating the Derivative of Shape Function
shape_prime = [sympy.diff(y, x) for y in shape_function]

# Defining Mapping Function
mapping = []
for i in range(0, num_nod_point - 1):
    mapping += [(1 - x) * nod_point[i] / 2 + (1 + x) * nod_point[i + 1] / 2]

if __name__ == "__main__"
  q = multiprocessing.Queue()
  p1 = multiprocessing.Process(target=da_k, args=(num_element, degree, h, shape_prime, px, mapping))
  p2 = multiprocessing.Process(target=da_m, args=(num_element, degree, h, shape_function, cx, mapping))
  p3 = multiprocessing.Process(target=da_f, args=(num_element, degree, h, shape_function, fx, mapping))
  p1.start()
  p2.start()
  p3.start()
  p1.join()
  p2.join()
  p3.join()
  global_k, total_local_k = q.get()
  global_m, total_local_m = q.get()
  global_f, total_local_f = q.get()

  print(global_k,global_m,global_f)

File "C:\Python\WinPython-64bit-3.5.1.1\python-3.5.1.amd64\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
RuntimeError: 
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

我最近正在学习多进程模块,我在我的代码中添加了多进程模块,但是当我点击“运行”时程序将运行3次,多进程部分必定有问题,因为其他部分工作正常在我添加多进程模块之前。有没有人可以帮我正确使用多进程模块?非常感谢!

更新:我检查了多进程代码,它工作正常,所以问题应该是关于队列,谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

RuntimeError是因为你在

末尾缺少一个冒号
if __name__ == "__main__"

应该是

if __name__ == "__main__":

如果缺少冒号,则执行代码时将运行该行之前的所有代码。此外,您似乎没有包含所有代码,也看不到您在任何地方放入任何内容。