以下代码来自python 2.6手册。
from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:', 'me')
print('parent process:', os.getppid())
print('process id:', os.getpid())
def f(name):
info('function f')
print('hello', name)
if __name__ == '__main__':
info('main line')
p = Process(target=f, args=('bob',))
p.start()
p.join()
这将创建以下堆栈跟踪:
Traceback (most recent call last):
File "threading.py", line 1, in <module>
from multiprocessing import Process
File "/usr/lib/python2.6/multiprocessing/__init__.py", line 64, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/usr/lib/python2.6/multiprocessing/util.py", line 287, in <module>
class ForkAwareLocal(threading.local):
AttributeError: 'module' object has no attribute 'local'
Exception AttributeError: '_shutdown' in <module 'threading' from '/home/v0idnull/tmp/pythreads/threading.pyc'> ignored
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
我对于为什么会发生这种情况完全无能为力,谷歌给了我很少的工作。
答案 0 :(得分:4)
我的机器上的代码运行正常:
Ubuntu 10.10,Python 2.6.6 64位。
但是您的错误实际上是因为您有一个名为'threading.py'的文件,您正在运行此代码(请参阅堆栈跟踪详细信息)。这导致命名空间不匹配,因为多处理模块需要“真实”线程模块。尝试将您的文件重命名为'threading.py'之外的其他内容并再次运行它。
也...你发布的例子不是来自Python 2.6文档......它来自Python 3.x文档。确保您正在阅读与您正在运行的版本相匹配的文档。