Let's say we use multiprocessing.Pool(n)
, is there a ways to find which of the n
processes we're in? To illustrate: In the following snippet I'd like to find a way to know what index of the process the job
function is. I'm not talking about the process number of the system, just a local enumeration of the processes spawned by Pool
.
from multiprocessing import Pool
def job(x):
process_idx = None # index 1,2,3,4?
print(f'job {x} process {process_idx}')
with Pool(4) as p:
p.imap(job, range(10))
我们可以通过使用找到进程索引(基于1
的索引)
process_idx = multiprocessing.current_process()._identity[0]
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.