从 mod_wsgi django 应用程序导入 numpy 时偶尔出现 ctypes 错误 [英] Occasional ctypes error importing numpy from mod_wsgi django app
问题描述
设置如下:
- 导入 ctypes 的 mod_wsgi 上的 Django (1.2) 应用
- Python 2.6.5
- Apache 2.2.3
- 禁用 SELinux
- 红帽 EL 5 64 位
- 某些文件系统挂载在 nfs 上
有时,当我重新启动 apache 时,我会在尝试导入 ctypes 时遇到导入错误.每个传入请求都失败并显示 500 错误.如果我重新启动 apache 通常一切都会重新开始工作.
Occasionally, when I restart apache I get an import error when it's trying to import ctypes. Every incoming request fails with a 500 error. If I restart apache usually everything just starts working again.
这是错误的堆栈跟踪:
Traceback (most recent call last):
File "/home/appfirst/django/django/core/handlers/base.py", line 80, in get_response
response = middleware_method(request)
-------------- A BUNCH OF DJANGO MIDDLEWARE STUFF HERE -------------
File "/home/appfirst/django/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/appfirst/backend/backend/streamer/views.py", line 6, in <module>
import appfirst.main.models as FEmodels
File "/home/appfirst/frontend/appfirst/main/models.py", line 27, in <module>
import numpy, math, mpmath
File "/usr/lib64/python2.6/site-packages/numpy/__init__.py", line 43, in <module>
import ctypeslib
File "/usr/lib64/python2.6/site-packages/numpy/ctypeslib.py", line 9, in <module>
import ctypes
File "/usr/lib64/python2.6/ctypes/__init__.py", line 546, in <module>
CFUNCTYPE(c_int)(lambda: None)
MemoryError
我认为这可能与这个错误有关,但我关闭了 SELinux,我认为这意味着这种情况永远不会发生:
I thought it might be related to this bug, but I have SELinux turned off which I thought would mean this case could never occur:
关于如何一致地重现和/或修复它的任何建议?这真的难倒我了!
Any suggestions on how to reproduce it consistently and/or fix it? This is really stumping me!
推荐答案
我也遇到了这个错误.就我而言,当我在 64 位 Linux 系统上的 Apache 下运行的 PHP 脚本中执行 Python 脚本时,就会发生这种情况.[正在运行的 Python 代码是 pypy 沙箱的前端.] 相同的代码在 32 位系统上运行良好,甚至在直接从命令行执行 PHP 脚本时也运行良好.我的修复"只是在 ctypes/init.py 中注释掉那行CFUNCTYPE(c_int)(lambda: None)".这是文件的最后一行,前面是下面的注释,表明程序员也不明白发生了什么!
I've run into this bug too. In my case it occurs when I exec a Python script from within a PHP script running under Apache on a 64-bit Linux system. [The Python code being run is the front-end to a pypy sandbox.] The same bit of code works fine on a 32-bit system and even works fine when the PHP script is executed directly from the command line. My "fix" has been simply to comment out that line "CFUNCTYPE(c_int)(lambda: None)" in ctypes/init.py. It's the last line of the file and is preceded by the following comment, showing that the programmer doesn't understand what's going on, either!
# XXX for whatever reasons, creating the first instance of a callback
# function is needed for the unittests on Win64 to succeed. This MAY
# be a compiler bug, since the problem occurs only when _ctypes is
# compiled with the MS SDK compiler. Or an uninitialized variable?
CFUNCTYPE(c_int)(lambda: None)
显然在 cpython 的某个地方存在一个更深层次的问题,但修复对我有用.
Clearly there's a deeper problem somewhere in cpython, but the fix works for me.
这篇关于从 mod_wsgi django 应用程序导入 numpy 时偶尔出现 ctypes 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!