同时使用多处理和ProcessPoolExecutor

时间:2017-11-16 11:26:33

标签: python-3.x multiprocessing deadlock process-pool

我正在尝试为python3.5创建一个可以并行执行重型计算机视觉算法的简单脚本。我在主进程中通过 multiprocessing.Process 创建了一个进程。 在该过程中,我创建了 concurrent.futures.ProcessPoolExecutor 。生成的进程将任务提交给 processPoolExecutor ,它完全正常。但是当我尝试停止并加入衍生进程时,它会挂起来加入。

另外,如果将processPoolExecuter替换为threadPoolExecuter,一切都能正常运行。我错过了什么?

以下是主要文件:

import multiprocessing as mp
import queue as Queue
import numpy as np
import cv2
from time import sleep
import executer_debug

def worker(queue):
    pExecutor = executer_debug.Worker()
    pExecutor.set()

    while True:
        print("-->{}<--".format(pExecutor.get()))
        sleep(1)

        try:
            income = queue.get_nowait()
            break
        except Queue.Empty:
            pass
        pExecutor.set()

    print("<1>{}<1>".format(pExecutor.get()))
    print("<2>{}<2>".format(pExecutor.get()))

def main():

    queue = mp.Queue()
    currProcess = mp.Process(target = worker, args=(queue,))
    currProcess.start()

    frame = np.zeros((480,640), dtype=np.uint8)

    while True:
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    print("stopped")
    queue.put("stop")

    currProcess.join()

if __name__ == "__main__":
    main()

这是第二个文件。代码非常简单,足以证明问题。

import collections
from concurrent.futures import ProcessPoolExecutor
from time import sleep
import multiprocessing as mp


def worker():
    return 1

class Worker():
    def __init__(self):
        self.workers_count = 4
        self.poolExecutor = ProcessPoolExecutor(max_workers = self.workers_count)
        self.executors = collections.deque()

    def set(self):
        self.executors.append(self.poolExecutor.submit(worker))

    def get(self):
        if len(self.executors) > 0:
            if self.executors[0].done():
                return self.executors.popleft().result()
            else:
                return 0
        else:
            return -1

谢谢!

0 个答案:

没有答案