Ubuntu 10.04 - Python 多处理 - “模块”对象没有属性“local”;错误

发布于 2024-09-27 20:59:10 字数 1713 浏览 3 评论 0原文

以下代码来自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

我完全不知道为什么会发生这种情况,并且谷歌给了我很少的帮助。

The following code is from the python 2.6 manual.

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()

This creates the following stack traces:

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

I'm completely clueless as to WHY this is happening, and google has given me very little to work with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2024-10-04 20:59:11

该代码在我的机器上运行良好:
Ubuntu 10.10、Python 2.6.6 64 位。

但您的错误实际上是因为您有一个名为“threading.py”的文件,您正在从中运行此代码(请参阅堆栈跟踪详细信息)。这导致命名空间不匹配,因为多处理模块需要“真正的”线程模块。尝试将文件重命名为“threading.py”以外的名称并再次运行它。

另外...您发布的示例不是来自Python 2.6 文档...它来自Python 3.x 文档。确保您正在阅读与您正在运行的版本相匹配的版本的文档。

that code runs fine on my machine:
Ubuntu 10.10, Python 2.6.6 64-bit.

but your error is actually because you have a file named 'threading.py' that you are running this code from (see the stack-trace details). this is causing a namespace mismatch, since the multiprocessing module needs the 'real' threading module. try renaming your file to something other than 'threading.py' and running it again.

also... the example you posted is not from the Python 2.6 docs... it is from the Python 3.x docs. make sure you are reading the docs for the version that matches what you are running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文