gpt4 book ai didi

python - multiprocessing.dummy 为什么 AttributeError : 'module' object has no attribute 'dummy'

转载 作者:行者123 更新时间:2023-12-01 22:55:38 25 4
gpt4 key购买 nike

我在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 xxxfrom xxx import 之间的区别,我想知道为什么它会引发 AttributeError。 我的问题是为什么在 import multiprocessing 后无法使用 multiprocessing.dummy

最佳答案

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'.

我猜这个子包是故意隐藏的,以防止人们意外使用它。

关于python - multiprocessing.dummy 为什么 AttributeError : 'module' object has no attribute 'dummy' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33774713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com