2012-06-11 33 views
3

我想從一個作爲子進程運行的腳本(通過apache服務器的python cgi腳本啓動)使用matplotlib繪圖(即編寫一個文件),但不管怎樣,該圖不是創建或服務器崩潰取決於我使用的後端。我認爲我應該使用「agg」,並在此情況下創建情節,但服務器崩潰(內部服務器錯誤)。這裏的日誌:Matplotlib子進程/後端問題

AH01215: /usr/lib/pymodules/python2.7/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display, referer: http://localhost/ 
AH01215: warnings.warn(str(e), _gtk.Warning), referer: http://localhost/ 
AH01215: /usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk.py:49: GtkWarning: IA__gdk_cursor_new_for_display: assertion `GDK_IS_DISPLAY (display)' failed, referer: http://localhost/ 
AH01215: cursors.MOVE   : gdk.Cursor(gdk.FLEUR),, referer: http://localhost/ 
malformed header from script 'submit.cgi': Bad header: ['/home/user/..., referer: http://localhost/ 

的CGI腳本好好嘗試一下取決於此刻的子進程的輸出,所以我真的不明白壞頭警告(並沒有這樣的問題時,不啓動子流程)。劇情腳本的開頭是這樣的:

import string 
import sys 
import os 
from math import * 
import tempfile 
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp() 
import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as matplot 
from matplotlib import mpl 

到目前爲止,我只是做與matplotlib,而不是作爲子相當簡單的情節,所以我會很感激你的幫助。

PS:我使用Ubuntu 11.04和Apache2的

回答

1

我懷疑你的劇情腳本輸出的東西stdout和Apache的解釋,作爲頭它從CGI期待。

當你調用腳本subprocess,將輸出重定向到dev空,這樣

from subprocess import Popen, STDOUT 
import os 

p = Popen(["ls","-l"], stdout=open(os.devnull, "w"), stderr=STDOUT) 
+1

它的工作原理,非常感謝你! – user1448268

+0

非常高興爲您服務! –