2017-09-04 4 views
2

프로세스 안에 multiprocessing.pool.map을 호출하고 싶습니다.프로세스에서 호출 된 멀티 프로세싱 풀 .map 차단

run() 함수 내에서 초기화되면 작동합니다. 인스턴스 생성시 초기화 될 때 인스턴스화되지 않습니다.

이 문제의 원인을 파악할 수 없습니까? 이 과정에서 어떤 일이 발생합니까? 그것의 기능에 대한 파이프 및 스레드를 사용하기 때문에 여러 프로세스에 의해 공유 할 수 없습니다 파이썬 3.6

from multiprocessing import Pool, Process, Queue 

def DummyPrinter(key): 
    print(key) 

class Consumer(Process): 
    def __init__(self, task_queue): 
     Process.__init__(self) 
     self.task_queue = task_queue 
     self.p = Pool(1) 

def run(self): 
    p = Pool(8) 
    while True: 
     next_task = self.task_queue.get() 
     if next_task is None: 
      break 

     p.map(DummyPrinter, next_task) # Works 
     #self.p.map(DummyPrinter, next_task) # Does not Work 
    return 

if __name__ == '__main__': 
    task_queue = Queue() 
    Consumer(task_queue).start() 

    task_queue.put(range(5)) 
    task_queue.put(None) 
+0

그럼 창을 사용한다고 가정 해 보겠습니다. –

+0

우분투에서 편집 된 – bold

+0

코드는 작동하지 않는 코드를 표시 할 수 있습니까? 나는 그런 문제를 해결했다고 생각한다. 나를 찾아 보자. –

답변

2

multiprocessing.Pool에입니다.

__init__ 메서드는 부모 프로세스에서 실행되지만 run 논리는 자식 프로세스에 속합니다.

Process 개체는 매우 직관적이어서 하위 분류를 사용하지 않는 것이 좋습니다.

다음과 같은 논리는 실제 책임 분담을 더 잘 보여줍니다.

def function(task_queue): 
    """This runs in the child process.""" 
    p = Pool(8) 
    while True: 
     next_task = self.task_queue.get() 
     if next_task is None: 
      break 

     p.map(DummyPrinter, next_task) # Works 

def main(): 
    """This runs in the parent process.""" 
    task_queue = Queue() 
    process = Process(target=function, args=[task_queue]) 
    process.start()