Is it possible to check in f if it's inside a Pool? Alternatively what is a good catch that only catches that specific error?
a = [[1, 2], [1, 2]]
def g(x):
return x
def f(x):
try:
with Pool(2) as p:
print(p.map(g, x))
except Exception as e:
print(type(e))
return x
with Pool(2) as p:
print(p.map(f, a))
Based on johndue's answer, but cleaner:
import multiprocessing
multiprocessing.current_process().daemon # True if already a subprocess
To avoid potential code duplication, I recommend to use a ThreadPool
as a drop-in replacement with the same interface:
import multiprocessing
import multiprocessing.pool
if multiprocessing.current_process().daemon:
pool_cls = multiprocessing.pool.ThreadPool
else:
pool_cls = multiprocessing.pool.Pool
with pool_cls() as pool:
pool.map()
If you want to apply Thread/Process specific parameters, eg, number of workers, have a look at functools.partial()
.
This works for me
from multiprocessing import current_process
if "daemon" in current_process()._config:
or
if "ForkPoolWorker" in str(current_process()):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.