亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在多處理函數中使用管理器(用于池)(Windows 10)

在多處理函數中使用管理器(用于池)(Windows 10)

慕標5832272 2023-09-26 16:41:43
我正在從多處理中學習池、管理器等。我想在我的函數中使用 Manager 中的命名空間。我從互聯網上獲取了一些突出顯示 Windows 中多處理管理器問題的代碼。這里是:"""How to share data in multiprocessing with Manager.Namespace()"""from multiprocessing import Pool, Managerimport numpy as np# Create manager object in module-level namespacemgr = 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 typeconfig.a = 1config.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 funcdef 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 thecurrent process has finished its bootstrapping phase.This probably means that you are not using fork to start yourchild processes and you have forgotten to use the proper idiomin the main module:    if __name__ == '__main__':        freeze_support()        ...The "freeze_support()" line can be omitted if the programis not going to be frozen to produce an executable.我認為,問題在于管理器突然插入了一個全局變量。您無法在 Windows 上執行此操作。正如你所看到的,我正在防守主力,但這還不夠。需要做的是將管理器以某種方式傳遞給函數(可能傳遞到映射變量中),但我不知道如何做到這一點。
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

是的,看起來將管理器創建為全局會導致 Windows 上出現問題。將其移至模塊 main 并將命名空間作為參數傳遞。Pool.map() 只允許將一個參數傳遞給工作線程,因此請將多個參數(包括命名空間)放入一個列表中。將參數列表的列表傳遞給 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)


查看完整回答
反對 回復 2023-09-26
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號