Python 3检查函数是否在多处理池中执行

时间:2021-04-07 20:44:10

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?

是否可以检查f是否在池内?或者,什么是只抓住特定错误的好捕获?

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))

1 个解决方案

#1


0  

This works for me

这对我有用

from multiprocessing import current_process
if "daemon" in current_process()._config:
or
if "ForkPoolWorker" in str(current_process()):

#1


0  

This works for me

这对我有用

from multiprocessing import current_process
if "daemon" in current_process()._config:
or
if "ForkPoolWorker" in str(current_process()):