简体   繁体   English

在Python的多处理包中,为什么有多处理.Pool和multiprocessing.pool.Pool?

[英]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 BaseContextcontext.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.Poolmultiprocessing.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM