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

前端 未结 5 1409
滥情空心
滥情空心 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:16

    Restructure your code so that the f() function is defined before you create instance of Pool. Otherwise the worker cannot see your function.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    from multiprocessing import Pool
    
    def f(x):
        return x*x
    
    p = Pool(1)
    p.map(f, [1, 2, 3])
    

提交回复
热议问题