用C和mod_wsgi在Apache中扩展Python/Django
我有一个关于用C代码扩展Python和mod_wsgi的问题。
我在一个Apache服务器上有一个Django应用,它会查询PostgreSQL数据库来生成报告。在某些报告中,系统会生成一个包含结果的CSV文件。为了创建这个CSV文件,有时候系统需要用Python处理超过二十万个记录,这显然非常慢。为了加快这个过程,我们编写了一个C模块来完成这项工作,这样速度提高了很多倍。我们尝试过使用ctypes和创建一个C的Python模块,这两种方法在运行服务器时都能正常工作,但在Apache和mod_wsgi下执行时却崩溃了。
在httpd-error.log中的错误信息是:
[Wed Jul 27 02:33:52 2011] [notice] child pid 44657 exit signal Segmentation fault (11)
有没有什么建议?
代码是:
# Creates the HttpResponse object with the appropriate CSV header.
response = HttpResponse(mimetype='application/x-zip-compressed')
response['Content-Disposition'] = \
'attachment; filename=' + filename + '.zip'
p0 = 'descarga_' + str(datetime.today()) + '.csv'
p1 = settings.DATABASES['default']['NAME']
p2 = settings.DATABASES['default']['USER']
#lib.generar(string_at(p0),p1,p2,string_at(str(init)),string_at(str(end)),string_at(str(provider)))
import generador
generador.generar(p0,p1,p2,str(init),str(end),str(provider))
在我们调用generador.generar()这个外部C模块时,它就崩溃了。
我还尝试过使用GDb,正如@GrahamDumpleton建议的那样,输出结果并不是很有用 :(
调试器启动后,当我点击执行调用C模块的链接时,又一次出现了段错误。
(gdb) run -X
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /usr/local/sbin/httpd -X
[New LWP 101064]
[New Thread 28501140 (LWP 101064)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 28501140 (LWP 101064)]
0x2847d423 in fwrite () from /lib/libc.so.7
在段错误之后,我在调试器中尝试了'where'命令,这就是它给出的结果:
(gdb) where
#0 0x2847d423 in fwrite () from /lib/libc.so.7
#1 0x293f8d09 in generar () from /ruta/al/codigo/generador.so
#2 0x28912caa in PyCFunction_Call () from /usr/local/lib/libpython2.7.so
#3 0x2896e49a in PyEval_EvalFrameEx () from /usr/local/lib/libpython2.7.so
#4 0x2897044b in PyEval_EvalCodeEx () from /usr/local/lib/libpython2.7.so
#5 0x288feafd in PyClassMethod_New () from /usr/local/lib/libpython2.7.so
#6 0x288d622c in PyObject_Call () from /usr/local/lib/libpython2.7.so
#7 0x2896c27a in PyEval_EvalFrameEx () from /usr/local/lib/libpython2.7.so
#8 0x2896fadc in PyEval_EvalFrameEx () from /usr/local/lib/libpython2.7.so
#9 0x2897044b in PyEval_EvalCodeEx () from /usr/local/lib/libpython2.7.so
#10 0x288fea0a in PyClassMethod_New () from /usr/local/lib/libpython2.7.so
#11 0x288d622c in PyObject_Call () from /usr/local/lib/libpython2.7.so
#12 0x288e4fd8 in PyClass_IsSubclass () from /usr/local/lib/libpython2.7.so
#13 0x288d622c in PyObject_Call () from /usr/local/lib/libpython2.7.so
#14 0x2893044c in _PyObject_LookupSpecial () from /usr/local/lib/libpython2.7.so
#15 0x288d622c in PyObject_Call () from /usr/local/lib/libpython2.7.so
#16 0x28968ec4 in PyEval_CallObjectWithKeywords () from /usr/local/lib/libpython2.7.so
#17 0x2889b229 in Adapter_run (self=0x28b4dd58, object=0x28c7d50c) at mod_wsgi.c:3841
#18 0x2889be50 in wsgi_execute_script (r=0x28cb4058) at mod_wsgi.c:6547
#19 0x2889de66 in wsgi_hook_handler (r=0x28cb4058) at mod_wsgi.c:9080
#20 0x08076b19 in ap_run_handler (r=0x28cb4058) at config.c:157
#21 0x08079dee in ap_invoke_handler (r=0x28cb4058) at config.c:376
#22 0x08084eb0 in ap_process_request (r=0x28cb4058) at http_request.c:282
#23 0x0808201b in ap_process_http_connection (c=0x28b201f0) at http_core.c:190
#24 0x0807de09 in ap_run_process_connection (c=0x28b201f0) at connection.c:43
#25 0x08089791 in child_main (child_num_arg=Variable "child_num_arg" is not available.
) at prefork.c:662
#26 0x080899e3 in make_child (s=0x28510f10, slot=0) at prefork.c:707
#27 0x0808a591 in ap_mpm_run (_pconf=0x2850f018, plog=0x2853d018, s=0x28510f10) at prefork.c:983
#28 0x08064195 in main (argc=676384792, argv=0x28b1e018) at main.c:739
1 个回答
试着设置:
WSGIApplicationGroup %{GLOBAL}
并强制应用程序在主Python解释器中运行。
可能你的C扩展没有正确编写,无法与Python的子解释器一起工作。
可以查看:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
你的代码也可能有问题,在mod_wsgi下运行时会暴露出问题,而在命令行Python中却不会。
可以查看:
http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Debugging_Crashes_With_GDB
了解如何调试崩溃发生的位置。
更新 1
在构建你的扩展模块时,为了帮助在gdb中调试,确保在编译扩展模块时没有开启优化,并且开启了调试选项。
对于使用setup.py构建的扩展模块,我发现有必要添加:
from distutils import sysconfig
dummy = sysconfig.get_config_vars('CFLAGS', 'OPT')
config_vars = sysconfig._config_vars
config_vars['CFLAGS'] = config_vars['CFLAGS'].replace(' -Os ', ' ')
config_vars['OPT'] = config_vars['OPT'].replace(' -Os ', ' ')
这样可以去掉 -Os 选项。是否使用 -Os 或 -O 取决于你的Python安装。
然后检查编译器选项中是否有 -g,当构建扩展模块时如果没有就添加上。
完成这些后,你就可以使用gdb来实际查看变量的值,获取实际的行号等等。