4

I met a strange thing in Python:

>>> import multiprocessing

>>> thread_pool = multiprocessing.dummy.Pool()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dummy'

However, when I try the following:

>>> from multiprocessing.dummy import Pool as ThreadPool
>>> ThreadPool()
<multiprocessing.pool.ThreadPool object at 0x7faf9308d4a8>

and

>>> import multiprocessing.dummy
>>> multiprocessing.dummy.Pool()
<multiprocessing.pool.ThreadPool object at 0x7faf9308d2e8>

Everything is OK. I know the difference between import xxx and from xxx import, I wonder why It raise AttributeError. My question is why I couldn't use multiprocessing.dummy after import multiprocessing?

WeizhongTu
  • 6,124
  • 4
  • 37
  • 51

1 Answers1

1

The multiprocessing.dummy package is not imported in multiprocessing.__init__.py. Hence,

>>> import multiprocessing

>>> thread_pool = multiprocessing.dummy.Pool()

does not work. Form the docstring:

# This package is intended to duplicate the functionality (and much of
# the API) of threading.py but uses processes instead of threads.  A
# subpackage 'multiprocessing.dummy' has the same API but is a simple
# wrapper for 'threading'.

I guess the subpackage has intentionally been hidden to prevent people from accidentaly using it.

thomas
  • 1,773
  • 10
  • 14
  • I try to add `'dummy'` into `__all__` in `multiprocessing/__init__.py`, `multiprocessing.dummy` still doesn't work. – WeizhongTu Nov 23 '15 at 13:30