我在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'
然而,当我尝试以下内容时:
>>> from multiprocessing.dummy import Pool as ThreadPool
>>> ThreadPool()
<multiprocessing.pool.ThreadPool object at 0x7faf9308d4a8>
和
>>> import multiprocessing.dummy
>>> multiprocessing.dummy.Pool()
<multiprocessing.pool.ThreadPool object at 0x7faf9308d2e8>
一切都好。
我知道import xxx
和from xxx import
之间的区别,我想知道它为什么会引发AttributeError。 我的问题是为什么在multiprocessing.dummy
之后我无法使用import multiprocessing
?
答案 0 :(得分:0)
multiprocessing.dummy
包中未导入multiprocessing.__init__.py
个包。因此,
>>> import multiprocessing
>>> thread_pool = multiprocessing.dummy.Pool()
不起作用。形成文档字符串:
# 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'.
我猜这个子包故意隐藏隐藏以防止人们意外地使用它。