yet another confusion with multiprocessing error, 'module' object has no attribute 'f'

前端 未结 5 1415
滥情空心
滥情空心 2020-11-28 07:53

I know this has been answered before, but it seems that executing the script directly \"python filename.py\" does not work. I have Python 2.6.2 on SuSE Linux.

Code:<

5条回答
  •  孤独总比滥情好
    2020-11-28 08:33

    This comes from the fact that with p = Pool(1) the main process forks processes (threads vs processes) before it creates the function f. As stated in Bartosz answer the spawned processes do not have access to the new function.

    def f1(x):
        ...
    
    p = Pool(1) # p is spawned and is now an independent process, knows f1
    
    def f(x): # p doesn't not share this object
        ...
    

提交回复
热议问题