Another confusion with a multiprocessor error, the 'module' object does not have the 'f' attribute

I know this was answered before, but it seems that the execution of the script directly by "python filename.py" does not work. I have Python 2.6.2 on SuSE Linux.

the code:

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

Command line:

 > python example.py Process PoolWorker-1: Traceback (most recent call last): File "/usr/lib/python2.6/multiprocessing/process.py", line 231, in _bootstrap self.run() File "/usr/lib/python2.6/multiprocessing/process.py", line 88, in run self._target(*self._args, **self._kwargs) File "/usr/lib/python2.6/multiprocessing/pool.py", line 57, in worker task = get() File "/usr/lib/python2.6/multiprocessing/queues.py", line 339, in get return recv() AttributeError: 'module' object has no attribute 'f' 
+49
python multiprocessing
May 6 '10 at 17:08
source share
4 answers

Restructure your code so that the f() function is defined before the pool is instantiated. 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]) 
+97
May 06 '10 at 17:15
source share

It works:

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

I am not 100% sure why your code does not work, but I think the reason is that the child processes launched by the multiprocessing module try to import the main module (have access to the methods you have defined) and the stanza if __name__ == "__main__" it is required not to execute the initialization code where you configure your pool.

+3
May 6 '10 at 17:15
source share

One possibility is that your python file has the same name as the module:

  • test.py
  • test /
    • __ __ INIT. RU

in pickle.py, you have an error:

  def find_class(self, module, name): # Subclasses may override this __import__(module) mod = sys.modules[module] # <- here mod will reference your test/__init__.py klass = getattr(mod, name) return klass 
+1
Oct 13
source share

The problem I encountered was resolved with if __name__ == "__main__" as indicated by Tamรกs; in Eclipse for Windows, the examples do not work under the interpreter. This is explained at http://docs.python.org/2/library/multiprocessing

0
Mar 30 '13 at 11:09
source share



All Articles