簡體   English   中英

在多處理功能中使用管理器(用於池)(Windows 10)

[英]Using the Manager (for Pool) in the function for multiprocessing (Windows 10)

我正在從多處理中學習池、管理器等。 我想在我的函數中使用 Manager 中的命名空間。 我從互聯網上刪除了一些代碼,這些代碼突出了 Windows 中多處理管理器的問題。 這里是:

"""How to share data in multiprocessing with Manager.Namespace()"""
from multiprocessing import Pool, Manager

import numpy as np


# Create manager object in module-level namespace
mgr = Manager()
# Then create a container of things that you want to share to
# processes as Manager.Namespace() object.
config = mgr.Namespace()
# The Namespace object can take various data type
config.a = 1
config.b = '2'
config.c = [1, 2, 3, 4]


def func(i):
    """This is a function that we want our processes to call."""
    # You can modify the Namespace object from anywhere.
    config.z = i
    print('config is', config)
    # And they will still be shared (i.e. same id).
    print('id(config) = {:d}'.format(id(config)))


# This main func
def main():
    """The main function contain multiprocess.Pool codes."""
    # You can add to the Namespace object too.
    config.d = 10
    config.a = 5.25e6
    pool = Pool(1)
    pool.map(func, (range(20, 25)))
    pool.close()
    pool.join()


if __name__ == "__main__":
    # Let's print the config
    print(config)
    # Now executing main()
    main()
    # Again, you can add or modify the Namesapce object from anywhere.
    config.e = np.round(np.random.rand(2,2), 2)
    config.f = range(-3, 3)
    print(config)

錯誤如下:

An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.

This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:

    if __name__ == '__main__':
        freeze_support()
        ...

The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.

我認為,問題在於經理被一個全局變量插入了。 您不能使用 Windows 執行此操作。 如您所見,我在守衛主力,但這還不夠。 需要做的是以某種方式將管理器傳遞給函數(可能傳遞給映射變量),但我不知道如何做到這一點。

是的,看起來像在 Windows 上創建全局管理器會導致問題。 將其移動到模塊 main 並將命名空間作為參數傳遞。 Pool.map() 只允許將一個參數傳遞給 worker,因此將多個參數(包括命名空間)放入一個列表中。 將參數列表列表傳遞給 Pool.map()。

我可能是錯的,但我認為您不應該期望/要求對象 ID 不更改。

from multiprocessing import Pool, Manager

import numpy as np


def func(a):
    """This is a function that we want our processes to call."""
    (config, i) = a
    # You can modify the Namespace object from anywhere.
    config.z = i
    print('config is', config)
    # And they will still be shared (i.e. same id).
    print('id(config) = {:d}'.format(id(config)))


# This main func
def main(config):
    """The main function contain multiprocess.Pool codes."""
    # You can add to the Namespace object too.
    config.d = 10
    config.a = 5.25e6
    pool = Pool(1)
    pool.map(func, list([config, i] for i in range(20,25)))
    pool.close()
    pool.join()


if __name__ == "__main__":
    # Create manager object in module-level namespace
    mgr = Manager()
    # Then create a container of things that you want to share to
    # processes as Manager.Namespace() object.
    config = mgr.Namespace()
    # The Namespace object can take various data type
    config.a = 1
    config.b = '2'
    config.c = [1, 2, 3, 4]

    # Let's print the config
    print(config)
    # Now executing main()
    main(config)
    # Again, you can add or modify the Namesapce object from anywhere.
    config.e = np.round(np.random.rand(2,2), 2)
    config.f = range(-3, 3)
    print(config)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM