0
我被困住了一個問題,我找不到一個好的答案。爲什麼multiprocessing.process在空隊列後不初始化?
我有一個腳本,從一個文件夾中獲取圖像,然後將其放入一個我命名池的隊列中。在而真循環我驗證是否有文件夾中的圖像。是的,我把這個圖像放到這個池Queue中,所以我創建了一個進程,他們會運行一個函數來驗證這些圖像是否有面,並且做其他不相關的事情。
我的問題來自代碼的不尋常comportament。如果在文件夾中有圖像,它們會爲每個進程螞蟻分配一個圖像。但如果圖像數量少於Processes,或者文件夾爲空,則當我將新圖像放入文件夾時,不會創建這些進程。
有任何解釋?
下面的代碼的相關部分:
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()
編輯你的問題裏面'face_search'如果任何'loop'條件。意味着你的過程是否等待,直到有新的圖像可用或在圖像處理後退出? – stovfl
圖像處理後退出。比while循環對下一張圖像進行同樣的處理。 –