2017-05-01 6 views
0

작업자 함수에 여러 args가 필요하므로 starmap을 사용하지만 tqdm을 사용하여 진행률을 어떻게 표시 할 수 있습니까?파이썬 다중 처리 starmap progressbar

from itertools import repeat 
from multiprocessing import Pool 

def func(i, a, b, c): 
    print(i, a, b, c) 

if __name__ == '__main__': 
    pool = Pool(thread_num) 
    a, b, c = 'a', 'b', 'c' 
    pool.starmap(func, zip(range(100), repeat(a), repeat(b), repeat(c))) 
    pool.close() 
    pool.join() 

그래서 사용자에게 tqdm을 표시하여 어떻게 미리 표시 할 수 있습니까?

+2

몇 가지가 아닌 하나의 튜플 인수를 받도록 함수를 변경할 수 있습니까? 그러면'starmap' 대신'imap'을 할 수있게되고, 메인 프로세스는 들어오는 결과를 반복하여 진행 바를 업데이트 할 수 있습니다. 불행히도 'istarmap'이 없습니다! 수정할 수없는 함수 (예 : 라이브러리에서 가져온 함수)에 대해서만이 작업을 수행하면 최상위 레벨에 정의 된 'def wrapper (tup) : func (* tup)'함수로 래핑 할 수 있습니다. – Blckknght

답변

0

다른 프로세스가 전달한 신호를 모니터하고 tqdm을 갱신하는 프로세스를 작성해야합니다. 당신을위한 최소한의 예 :

from multiprocessing import Queue, Pool, Process 

def listener(q, num): 
    tbar = tdqm(total = num) 
    for i in iter(q.get, None): 
     tbar.update() 
    tbar.close() 

def worker(q): 
    do something. 
    queue.put(1) 

if __name__ == "__main__": 
    .... 
    q.put(None) #when you finish your work, put a None signal to finish the listener.