Python Multiprocessing 不允许在创建进程后在主进程中打印
Python Multiprocessing disallows printing in main process after processes are created
我试图确保在开始流程之前正确设置了此代码。添加一些打印语句后,我发现只有 'outer' 和 'inner' 正在打印,我不明白为什么其他打印语句没有执行。
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:\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
是应该在新进程中执行的功能。因此,根据documentation,对构造函数的调用应该是:
process = multiprocessing.Process(target=scrape_retailer_product,
args=(retailer_products[i+j],))
如果你想捕获所有多处理异常,你应该捕获 multiprocessing.ProcessError
。根据 documentation,它是所有多处理异常的基础 class。
我试图确保在开始流程之前正确设置了此代码。添加一些打印语句后,我发现只有 'outer' 和 'inner' 正在打印,我不明白为什么其他打印语句没有执行。
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:\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
是应该在新进程中执行的功能。因此,根据documentation,对构造函数的调用应该是:
process = multiprocessing.Process(target=scrape_retailer_product,
args=(retailer_products[i+j],))
如果你想捕获所有多处理异常,你应该捕获 multiprocessing.ProcessError
。根据 documentation,它是所有多处理异常的基础 class。