使用从另一个文件导入时并发错误

时间:2020-06-14 03:54:37

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

我有一段玩具代码,可以做一些虚拟工作来测试并行化。该代码可以正常工作,但是如果我尝试从另一个文件导入一个类,则该代码将失败。它给我错误BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

#from Geometry import * <- this line causes the code to break
import concurrent.futures
import itertools
import random
import time

class ConProc:

    def dummy(self, param):
        time.sleep(random.random() * 3) # simulate a longer job
        return param[0] * param[1]

    def main(self):
        ht_iterator = range(4)
        wt_iterator = range(5)
        paramlist = list(itertools.product(ht_iterator, wt_iterator))

        with concurrent.futures.ProcessPoolExecutor() as executor:
            ret = executor.map(self.dummy, paramlist)

            for result in ret:
                print(result)

if __name__ == '__main__':
    cp = ConProc()
    cp.main()

Geometry.py-

的内容
import math
import numpy as np


class vector(np.ndarray):
    def __new__(cls, input_array):
        obj = np.asarray(input_array).view(cls)
        return obj

    def __array_finalize__(self, obj):
        if obj is None: return



class ray(vector):
    pass

class sphere:

    def __init__(self, center, radius, material):
        self.center = center
        self.radius = radius
        self.material = material

    def intersects(self, ray, ray_direction):
          # import pdb; pdb.set_trace()
          sphere_to_ray = ray - self.center
          b = np.dot(2*ray_direction, sphere_to_ray)
          c = np.dot(sphere_to_ray, sphere_to_ray) - self.radius*self.radius
          disc = b * b - 4 * c

          if disc >= 0:

              dist = (-b - math.sqrt(disc)) / 2
              if dist > 0:
                  return dist

          return None

    def normal(self, hit_pos):
        return (hit_pos - self.center) / np.linalg.norm(hit_pos - self.center)

我发现这个问题令人费解,因为即使我实际上没有使用任何来自Geometry的东西,也会发生此错误。

有时我还会收到此错误BrokenProcessPool: A child process terminated abruptly, the process pool is not usable anymore

其他信息: 堆栈跟踪-

Traceback (most recent call last):

  File "C:\Users\test_conc.py", line 42, in <module>
    test = cp.main()

  File "C:\Users\test_conc.py", line 35, in main
    for result in ret:

  File "C:\ProgramData\Anaconda3\lib\concurrent\futures\process.py", line 476, in _chain_from_iterable_of_lists
    for element in iterable:

  File "C:\ProgramData\Anaconda3\lib\concurrent\futures\_base.py", line 586, in result_iterator
    yield fs.pop().result()

  File "C:\ProgramData\Anaconda3\lib\concurrent\futures\_base.py", line 432, in result
    return self.__get_result()

  File "C:\ProgramData\Anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception

BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

0 个答案:

没有答案
相关问题