Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current process id when using `concurrent.futures.ProcessPoolExecutor`

I am using concurent.futures.ProcessPoolExecutor has a high level API to multiprocessing.

I want to identify the current process in the worker functions.

With the low level API multiprocessing I can do it like this.

import multiprocessing
  
def worker():
    print(multiprocessing.current_process())

Is there a current_process() pendant when using workers with the ProcessPoolExecutor()?

like image 839
buhtz Avatar asked Aug 27 '26 12:08

buhtz


1 Answers

Since each execution happens in a separate process, you can simply do

import os
  
def worker():
  # Get the process ID of the current process
  pid = os.getpid()
  ..
  .. do something with pid

For example,

from concurrent.futures import ProcessPoolExecutor
import os
import time

def task():
    time.sleep(1)
    print("Executing on Process {}".format(os.getpid()))

def main():
    with ProcessPoolExecutor(max_workers=3) as executor:
        for i in range(3):
            executor.submit(task)

if __name__ == '__main__':
    main()

➜ python3.9 so.py
Executing on Process 71137
Executing on Process 71136
Executing on Process 71138

Note that if the task in hand is small and executed fast enough, your pid might stay the same. Try it out by removing the time.sleep call from my example.

like image 194
Chen A. Avatar answered Aug 30 '26 00:08

Chen A.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!