The easiest way is to use the multiprocessing module:
class FindIntersectionsWrapper(object): def __init__(self, ent, collidable): self.ent = ent self.collidable = collidable def __call__(self, dims): line1, line2 = dims return find_intersections( line1 + self.ent.position, self.ent.velocity, self.ent.acceleration, line2 + self.collidable.position, self.collidable.velocity, self.collidable.acceleration, self.ent, self.collidable) def entity_intersections(ent, collidable): find_inter = FindIntersectionsWrapper(ent, collidable) pool = multiprocessing.Pool() return pool.map(find_inter, product(ent.shape, collidable.shape))
The helper function find_intersections_wrapper() needed because Pool.map() expects a function with a single argument.
You might want to migrate the pool creation from entity_intersections() to have the overhead of creating a process pool only once.
Change The class is used instead of closing, since the caller passed to Pool.map() must be selected on Windows.
source share