![](/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.