【问题标题】:The program run three times when I using multiprocessing module当我使用多处理模块时程序运行了三次
【发布时间】:2016-01-01 05:04:10
【问题描述】:
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次,肯定是多进程部分有问题,因为其他部分都可以在我添加多进程模块之前。有谁可以帮助我正确使用多进程模块?非常感谢!

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

【问题讨论】:

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


    【解决方案1】:

    RuntimeError 是因为您在末尾缺少一个冒号

    if __name__ == "__main__"
    

    应该是

    if __name__ == "__main__":
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多