我试图确保在开始进程之前正确设置了此代码。添加一些打印语句后,我发现只有"外部"和"内部"正在打印,我不明白为什么其他打印语句没有执行。
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()
问题是您正在捕获所有异常。因此,您的代码没有将正确的参数传递给Process
构造函数(生成AssertionError
(,但您的catch
语句静默地处理异常。
当前的例外情况是:
Traceback (most recent call last):
File "C:UsersMiguelAngelDownloadstest.py", line 19, in <module>
process = multiprocessing.Process(scrape_retailer_product, args=(retailer_products[i+j]))
File "C:UsersMiguelAngelAppDataLocalProgramsPythonPython38-32libmultiprocessingprocess.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
是应该在新进程中执行的功能。因此,根据文档,对构造函数的调用应该是:
process = multiprocessing.Process(target=scrape_retailer_product,
args=(retailer_products[i+j],))
如果要捕获所有多处理异常,则应捕获multiprocessing.ProcessError
。根据文档,它是所有多处理异常的基类。