2013-04-21 68 views
0

這個問題相當有線,它讓我瘋狂。任何幫助表示讚賞!我們有一些底層C代碼,它們將被Python模塊(Python版本2.6,Django框架,版本1.4)調用。我們通過SWIG 2.0.9構建了Python綁定。我們從python manage.py shell提示中使用了它們。他們工作得很好。SWIG:python模塊只有在部署到Apache時纔會崩潰

如果我們運行python manage.py runserver 0.0.0.0:8001,一切仍然正常。但是,當我們將應用程序部署爲Apache虛擬主機時,Python綁定突然失敗(其他網頁沒有使用C代碼就沒有問題)。它的網址類似http://hostname.com/basic。下面是我們的conf文件:

#Listen 80 
<VirtualHost *:80> 
    ServerName hostname.com/basic 
    ServerAdmin [email protected] 

    ProxyRequests Off 
    <Proxy *> 
    Order deny,allow 
    Allow from all 
    </Proxy> 

    DocumentRoot /home/browser/BASIC/basic 

    Alias /static/admin /home/browser/BASIC/_py/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/contrib/admin/static/admin 
    Alias /static /home/browser/BASIC/basic/static 

    WSGIScriptAlias /basic /home/browser/BASIC/basic/basic.wsgi 
    ErrorLog /home/browser/BASIC/basic/log/basic.error.log 

    <Directory /home/browser/BASIC/basic> 
    <Files basic.wsgi> 
     Order deny,allow 
     Allow from all 
    </Files> 
    </Directory> 

    LogLevel info 
    CustomLog /home/browser/BASIC/basic/log/basic.access.log combined 

我們需要查詢的C代碼的兩倍。第一個仍然工作並返回正確的結果。但第二次審判只是崩潰。代碼停止並且沒有返回。一些代碼片段:

from some.basic import rmq  # this is the Python binding created by SWIG 
class Maxi: 
    @staticmethod 
    @contextmanager 
    def open(rmqfile): 
    driver = None 
    try: 
     driver = Maxi(rmqfile) 
     yield driver 
    finally: 
     if driver: driver.close() 

    def __init__(self, rmqfile): 
    with open(rmqfile) as f: 
     self.rmqfile = yaml.load(f) 
    self.handlers = dict() 

    # The rmq.i: 
    # int  rmq_query (rmq_track t, unsigned int p, unsigned int q); 
    # rmq_track rmq_load (char filename[]); 
    def query(self, chrom, start, end): 
    h = rmq.rmq_load(self.rmqfile[chrom])) 
    rs = rmq.rmq_query(h, start-1, end-1)  # It works and result is correct 
    rs = rmq.rmq_query(h, start-1+1, end-1+1) # !!! Crashes here !!! 
    return rs 

    def close(self): 
     for h in self.handlers.itervalues(): 
      rmq.rmq_unload(h) 

# ==== Django views code ==== 
driver = Maxi('yaml_config_file_to_load') 
result.append(driver.query('chr1', 1, 100000) 

我猜想它是由一些文件權限問題引起的。但即使我將所有Python綁定文件設置爲777,在作爲Apache虛擬主機工作時仍會失敗。

我確實嘗試刪除dirver.close()聲明,仍然失敗。在代碼追蹤之後,close()方法永遠不會被調用。

也許有一些配置會導致python綁定的生命週期管理不當。但我無法確定原因。

請幫忙。非常感謝。

==================

更新:

閱讀http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API後,我添加到我的conf文件:

WSGISocketPrefix /var/run/wsgi 
WSGIApplicationGroup %{GLOBAL} 
WSGIPythonHome /home/browser/BASIC/_py 
<VirtualHost *:80> 
    ...... 
    WSGIApplicationGroup %{GLOBAL} 
    WSGIDaemonProcess hostname.com/basic processes=2 threads=15 display-name=%{GROUP} 
    WSGIProcessGroup hostname.com/basic 
    ...... 
</VirtualHost> 

但它報告錯誤:

Premature end of script headers: basic.wsgi 

回答

0

終於來了!經過大量搜索並嘗試了很多之後,我解決了這個問題。這裏是我的conf文件:

# Important! My http.conf does not contain this line. So Apache seems to 
# load the mod_wsgi.so from somewhere else, which is unluckily not compatible 
# with my C codes. 
LoadModule wsgi_module modules/mod_wsgi.so 
# Place to store file for socket communications. I created the folder manually. 
WSGISocketPrefix /var/run/wsgi 
<VirtualHost *:80> 
    ...... 
    # My wsgi configuration file 
    WSGIScriptAlias /basic /home/browser/BASIC/basic/basic.wsgi 
    # Make the virtual host using only one Python sub interpreter. 
    WSGIApplicationGroup %{GLOBAL} 
    # Make it run under the daemon mode 
    WSGIDaemonProcess hostname.com/basic processes=1 threads=15 display-name=%{GROUP} \ 
    python-path=/home/browser/BASIC/_py/lib/python2.6/site-packages 
    WSGIProcessGroup hostname.com/basic 
    ...... 
</VirtualHost> 
+0

但是有哪些具體的變化有幫助? – 2013-04-23 21:05:26

+0

@ graham-dumpleton在測試中,如果我刪除了'LoadModule',將會報告'腳本頭部提前結束'。如果我刪除'WSGISocketPrefix',它會報告'權限被拒絕:無法連接到WSGI守護進程'。如果我不使用'守護進程模式',錯誤保持不變。如果我刪除'WSGIApplicationGroup%{GLOBAL}',它也可以工作。其實我也需要深入的解釋。 – Joy 2013-04-24 02:17:36

1
+0

我更新我的問題,你能幫忙嗎?謝謝。 – Joy 2013-04-22 02:56:28

+0

這通常表示它仍然崩潰。嘗試使用gdb來確定它在哪裏崩潰。請參閱http://code.google。com/p/modwsgi/wiki/DebuggingTechniques#Debugging_Crashes_With_GDB – 2013-04-22 10:47:41