简体   繁体   中英

How to get currently running processes names from multiprocessing module in python

I know that in multithreading, there is enumerate method which I can use to get names of all current running threads. But for multiprocessing, what is the equivalent in the non pool like process call:

p1 = multiprocessing.Process(name='P1',
                    target=self.get_data,
                    args=(url,))

Ex, if I create above process, and one more process as above with 'P2', then the required snippet should return P1 and P2

You can use multiprocessing.active_children() which returns a list of multiprocessing.Process objects.

For example:

for p in multiprocessing.active_children():
    print(f"child {p.name} is PID {p.pid}")

If you didn't use the name= argument to Process() the default names are, eg, Process-1 , Process-2 , etc.

Note that this has the same limitation of threading.enumerate() in that it only identifies processes that have been started with .start() but have not yet terminated.

Also note this bit from the Python documentation:

Calling this has the side effect of “joining” any processes which have already finished

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM