views:

760

answers:

1

I tried to copy this example from this Multiprocessing lecture by jesse noller (as recommended in another SO post)[http://pycon.blip.tv/file/1947354?filename=Pycon-IntroductionToMultiprocessingInPython630.mp4]

But for some reason I'm getting an error, as though it's ignoring my function definitions: I'm on Windows XP (win32) which I know has restrictions with regards to the multiprocessing library in 2.6 that requires everything be pickleable

from multiprocessing import Process
import time

def sleeper(wait):
    print 'Sleeping for %d seconds' % (wait,)
    time.sleep(wait)
    print 'Sleeping complete'

def doIT():    
    p = Process(target=sleeper, args=(9,))
    p.start()
    time.sleep(5)
    p.join()

if __name__ == '__main__':
    doIT()

Output:

Evaluating mypikklez.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python26\lib\multiprocessing\forking.py", line 342, in main
    self = load(from_parent)
  File "C:\Python26\lib\pickle.py", line 1370, in load
    return Unpickler(file).load()
  File "C:\Python26\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python26\lib\pickle.py", line 1090, in load_global
    klass = self.find_class(module, name)
  File "C:\Python26\lib\pickle.py", line 1126, in find_class
    klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'sleeper'

The error causing the issue is : AttributeError: 'module' object has no attribute 'sleeper'

As simple of a function as it is I can't understand what would be the hold up.

This is just for self-teaching purposes of basic concepts. I'm not trying to pre-optimize any real world issue.

Thanks.

+3  A: 

Seems from the traceback that you are running the code directly into the python interpreter (REPL).

Don't do that. Save the code in a file and run it from the file instead, with the command:

python myfile.py

That will solve your issue.


As an unrelated note, this line is wrong:

print 'Sleeping for ' + wait + ' seconds'

It should be:

print 'Sleeping for %d seconds' % (wait,)

Because you can't concatenate string and int objects (python is strongly typed)

nosklo
I'm running from the windows command line
your fix is absolutely correct and 100% pythonic (not to mention scalable/readable/extensible, but I have often wondered if that form is less efficient than: print "Sleeping for ", a, " seconds" or even "Sleeping for " + str(a) + " seconds"
Shane C. Mason
@unknown: remove the pickle.py file you have created, and the corresponding .pyc, from your project folder.
nosklo
@Shane: Who cares if it's 0.0002 miliseconds slower? Such a small difference in performance shouldn't even be thought about.
musicfreak
@musicfreak: it is not slower. It is a lot faster.
nosklo
@Shane C. Mason: It is a lot more scalable/readable/extensible. Just benchmark it yourself. String building with + is quadratic time instead of linear. Here's a good read for you: jaynes.colorado.edu/PythonIdioms.html
nosklo
@nosklo -- can you tell me why running it from command (with your fixes) works but having it WingIDE "evaluate" my file in the interpreter presents issues ? I like to run my code every few lines (since I'm still learning) to check for errors -- would seem cumbersome to launch it this way unnecessarily in the future? What can be run in the REPL(?) and not in the REPL?
@unknown: because the other process has to import your file in order to run the function in it. If it is not saved, there's no way for the other process to import it.
nosklo