[英]In Python's multiprocessing package, why is there multiprocessing.Pool and multiprocessing.pool.Pool?
While learning about Python's multiprocessing
package (for Python 3.4 ), I noticed multiprocessing.Pool
is defined in the Class BaseContext
in context.py
. 在学习Python的multiprocessing
包(对于Python 3.4 )时,我注意到了multiprocessing.Pool
BaseContext
在context.py
中的Class BaseContext
中定义。 This definition is 这个定义是
def Pool(self, processes=None, initializer=None, initargs=(),
maxtasksperchild=None):
'''Returns a process pool object'''
from .pool import Pool
return Pool(processes, initializer, initargs, maxtasksperchild,
context=self.get_context())
Thus, it ends up calling multiprocessing.pool.Pool
defined in pool.py
. 因此,结束调用multiprocessing.pool.Pool
中定义pool.py
。
Why does the multiprocessing
package define both multiprocessing.Pool
and multiprocessing.pool.Pool
? 为什么multiprocessing
包定义了multiprocessing.Pool
和multiprocessing.pool.Pool
?
context.py
contains OS-dependent code. context.py
包含依赖于操作系统的代码。 The Pool
and many other values are defined differently depending on whether sys.platform == 'win32'
or not. Pool
和许多其他值的定义不同,具体取决于sys.platform == 'win32'
。
The pool.py
module contains code related the creation of a Pool given a context . pool.py
模块包含与给定上下文创建池相关的代码。
By organizing the code in this way, the developer manages to write pool.py
in an OS-agnostic way. 通过以这种方式组织代码,开发人员设法以与操作系统无关的方式编写pool.py
There are no if sys.platform ...
statements in pool.py, for example. 例如,pool.py中没有if sys.platform ...
语句。
The __init__.py
contains: __init__.py
包含:
globals().update((name, getattr(context._default_context, name))
for name in context._default_context.__all__)
__all__ = context._default_context.__all__
which copies values from context._default_context
into the multiprocessing
packages' namespace. 它将来自context._default_context
值复制到multiprocessing
包的命名空间中。 This is how multiprocessing.Pool
gets defined. 这就是multiprocessing.Pool
的定义方式。
As a user of the multiprocessing
package, you are expected to access the Pool via multiprocessing.Pool
, although it is possible to use multiprocessing.pool.Pool
too. 作为multiprocessing
程序包的用户,您需要通过multiprocessing.Pool
访问池,尽管也可以使用multiprocessing.pool.Pool
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.