[英]multiprocess.Queue.get() needs very long time in python
我需要通過迭代數據來獲取真正的大數據塊。 我總共需要幾百萬次迭代。 所以我認為壓制會加快我的進程,而且幾乎做到了。 我使用subprocess.Queue
來調用不同的線程,這實際上工作得很好,但是當我調用 *subprocess.Queue.get()` 時,程序需要很長時間才能獲得結果。 也許我做錯了什么。 這是我的最小示例:
def get_losses(self, tags=None):
return_dict = {}
output_list = multiprocessing.Queue()
process_list = []
# Create quese definition
for experiment, path in self.tf_board_dicts.items():
t = multiprocessing.Process(target=self._load_vec_from_tfboard, args=(path, tags, experiment))
process_list.append(t)
print("Starting subprocesse with a total of {} workers. \n These are {}".format(len(process_list),
process_list))
# Run processes
for p in process_list:
p.start()
# Exit the finished threads
for p in process_list:
p.join()
print("All subprocesses are termianted")
# Get results
results = [output_list.get() for p in process_list]
print("All losses are gathered: {}".format([tup[0] for tup in results]))
# Create dict
for experiment_losses in results:
return_dict[experiment_losses[0]] = experiment_losses[1]
return return_dict
您可以在此處找到隊列無限時間問題的答案: Python 進程未加入
發生這種情況是因為當大量數據被推入
Queue
時,Queue
在內部使用緩沖區。 在該緩沖區被刷新之前,寫入Queue
的進程無法退出,直到您開始將內容從Queue
取出時才會退出。 因此,由於您在從正在寫入的Queue
對象中提取任何內容之前嘗試加入所有進程,因此它們無法退出,因此join
掛起。 您可以通過在對進程調用join
之前排空Queue
來解決此問題。 – 達諾 2014 年 9 月 25 日 16:16
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.