27

I am trying to figure a way to get the number of processes directly from an instance of multiprocessing.Pool class in Python.. Is there a way to do it?

The documentation doesn't show anything related.

Thanks

gc5
  • 9,468
  • 24
  • 90
  • 151

1 Answers1

34

You can use _processes attribute:

>>> import multiprocessing
>>> pool = multiprocessing.Pool()
>>> pool._processes
8

The return value is same for multiprocessing.cpu_count() unless you specified process count when creating Pool object.

>>> multiprocessing.cpu_count()
8
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • It was really obvious.. I don't know why I was thinking that underscore prefixed attributes were not accessible.. Thanks – gc5 Dec 03 '13 at 14:53
  • 5
    Anyway, it is somehow possible that using it may break my code in the future? I mean, http://stackoverflow.com/a/14671542/41977 if they put a private attribute with no getter method, it can be possible that it is not the safest solution? – gc5 Dec 03 '13 at 15:01
  • If you develop your system on Python 3.x and stay on that version, you will be fine. Of course, if you upgraded the Python version your code is running on, you should check the Python changelog first, even though I doubt that there will be a change in this relatively robust part of the Python SDK (but that is just my personal opinion). tl;dr: Always check the change log first before upgrading your Python version. – pedjjj Nov 27 '19 at 13:49