[英]working with multiprocessing using map() in python code in bigdata
我正在嘗試從存儲在data.file中的網址中獲取一些值(我使用提取函數獲取),並且該文件中大約有3000000個網址鏈接。 這是我的代碼段,
from multiprocessing import Pool
p = Pool(10)
revenuelist = p.map(extract, data.file )
但是問題是,由於存在互聯網連接,如果存在連接問題,這是代碼再次運行。 如何為我的代碼增加容錯能力(即存儲中間結果,以避免重復執行相同的任務)。
一個非常簡單的解決方案是使用文件存儲您的當前狀態。 使用try ... finally處理失敗:
with open(FILENAME) as f:
current = int(f.read() or 0)
if current:
skip_lines(current)
try:
with Pool() as pool:
results = pool.imap(extract, data.file)
for result in results:
do_something(result)
current += 1
finally:
with open(FILENAME, "w") as f:
f.write(str(current))
另請參見: 'concurrent.futures' (比multiprocessing.Pool涼得多)。
更好的解決方案是使用數據庫完全跟蹤您的進度,和/或使用更好的任務隊列(例如celery )執行您的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.