![](/img/trans.png)
[英]python multiprocessing: How to modify a dictionary created in the main process from a subprocess?
[英]Python Multiprocessing disallows printing in main process after processes are created
我試圖確保在開始流程之前正確設置了此代碼。 添加一些打印語句后,我發現只有“外部”和“內部”在打印,我不明白為什么其他打印語句沒有執行。
import multiprocessing
from itertools import product
retailer_ids = [41, 499] # defined retailers
product_ids = [4, 5, 10, 11, 12] # ProductIDs to search on
NUMBER_OF_PROCESSES = 2
retailer_products = list(product(retailer_ids, product_ids))
# Start processing the retailer/product combinations
for i in range(0, len(retailer_products), NUMBER_OF_PROCESSES):
print('outer')
try:
current_processes = []
for j in range(0, NUMBER_OF_PROCESSES):
print('inner')
process = multiprocessing.Process(scrape_retailer_product, retailer_products[i+j])
#process.start()
current_processes.append(process)
# wait for current process to finish before starting more
print('waiting for processes to complete')
for p in current_processes:
p.join()
print('completed')
# something bad happened during process creation or a
# a scrape process returned with an exception it could not handle
except Exception as e:
for p in current_processes:
p.terminate()
print('term')
exit()
問題是您正在捕獲所有異常。 因此,您的代碼沒有將正確的 arguments 傳遞給Process
構造函數(生成AssertionError
),但是您的catch
語句正在靜默處理異常。
當前的例外是:
Traceback (most recent call last):
File "C:\Users\MiguelAngel\Downloads\test.py", line 19, in <module>
process = multiprocessing.Process(scrape_retailer_product, args=(retailer_products[i+j]))
File "C:\Users\MiguelAngel\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 82, in __init__
assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now
我想scrape_retailer_product
是應該在新進程中執行的 function。 因此,根據文檔,對構造函數的調用應該是:
process = multiprocessing.Process(target=scrape_retailer_product,
args=(retailer_products[i+j],))
如果要捕獲所有多處理異常,則應捕獲multiprocessing.ProcessError
。 根據文檔,它是所有多處理異常的基礎 class 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.