问题描述
我试图从jesse noller的这个Multiprocessing讲座中复制这个示例(如另一篇SO文章所推荐)[ http://pycon.blip.tv/file/1947354?filename=Pycon-IntroductionToMultiprocessingInPython630.mp4]
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]
但是由于某种原因,我遇到了一个错误,好像它忽略了我的函数定义:我使用的是Windows XP(win32),我知道它对2.6中的多处理库有限制,要求所有内容都是可腌制的
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()
输出:
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'
导致该问题的错误是:AttributeError:'模块'对象没有属性'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.
谢谢.
推荐答案
从追溯中看来,您正在直接将代码运行到python解释器(REPL)中.
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
那将解决您的问题.
作为不相关的注释,此行是错误的:
As an unrelated note, this line is wrong:
print 'Sleeping for ' + wait + ' seconds'
应该是:
print 'Sleeping for %d seconds' % (wait,)
因为您无法连接字符串和int对象(python是强类型的)
Because you can't concatenate string and int objects (python is strongly typed)
这篇关于Python 2.6 Win32(xp)上的Python多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!