2017-06-01 3 views
0

私は質問に固執しており、私は良い答えを見つけることができません。マルチプロセッシングがなぜ空のキューの後で初期化されないのですか?

私は、フォルダからイメージを取得し、プールに名前を付けたキューに入れるスクリプトを持っています。 で真の場合ループiはフォルダに画像があるかどうかを確認します。はい、私はこのイメージを取って、このプールのキューに入れて、私はこれらの画像に顔を持っているかどうかを確認する機能を実行するプロセスを作成し、他の無関係なものを行います。

私の質問は、コードから珍しいcomportamentに来る。フォルダ内にイメージがある場合は、プロセスの各プロセスに対してイメージが1つずつ配布されます。プロセスよりも画像が少ない場合、またはフォルダが空の場合は、新しい画像をフォルダに入れるとプロセスが作成されません。

これにはどのような説明がありますか?

は、ここでは、コードの関連部分です:

def face_search(pool, qtd_pool): 
    # Do face recognition and move files 
    # When files moved, the folder with images get empty until i put new images 
    # if there's no face, the image is deleted from disk 
    # At the end, it return True and enter in the next image loop 

if __name__ == '__main__': 
    #irrelevant stuff 
    while true: 
    pool_get = os.listdir(/some_directory/) 
    qtd_pool = len(pool_get) 
    pool = Queue() 

    for image in pool_get: 
     pool.put('/some_directory/'+image) 

    # down below i create the Process, and join then when finished. They would be created for every loop, right? Why they don't act like that? 
    procs = [Process(target = face_search, args=(pool, qtd_pool,)) for i in xrange(nthreads)] 

    for p in procs: p.start() 
    for p in procs: p.join() 
+0

編集。イメージは新しいイメージが利用可能になるまで待機するか、イメージが処理された後に終了しますか? – stovfl

+0

画像処理後に終了します。真のループが次の画像と同じ処理をするよりも、 –

答えて

0

質問:...私は、フォルダに新しい画像を入れたときにプロセスが作成されていません。

あなたはフォルダが空の場合は無条件で、whileループ内でこのすべてを行います。 新しいプロセスを作成してシステムに過負荷をかけると仮定します。

に一度プロセスを作成し、新しいイメージが準備できるまで待つようにします。

のPythonでテスト
def face_search(exit_process, job_queue): 
    while not exit_process.is_set(): 
     try: 
      job = job_queue.get_nowait() 
      # Do image processing 

     except queue.Empty: 
      time.sleep(0.5) 

    exit(0) 

def process_images(job_queue): 
    path = '.' 
    for fname in os.listdir(path): 
     job_queue.put(os.path.join(path, fname)) 


if __name__ == '__main__': 
    exit_process = mp.Event() 
    job_queue = mp.Manager().Queue() 

    pool = [] 
    for n in range(mp.cpu_count()): 
     p = mp.Process(target=face_search, args=(exit_process, job_queue)) 
     p.start() 
     pool.append(p) 
     time.sleep(0.1) 

    process_images(job_queue) 

    # Block until all jobs done 
    while not job_queue.empty(): 
     time.sleep(1) 

    # Stop Processes 
    exit_process.set() 

:face_search` ``内部loop`条件でご質問があれば3.4.2および2.7.9

関連する問題