Matplotlib subprocess/backend issue
I want to plot (i.e. write a file) with matplotlib from a script that runs as a subprocess (launched by a python cgi-script from an apache server), but somehow either the plot isn't created or the server crashes depending on the backend I use. I think I should be using "agg", and the plot is created in that case, but the server crashes then (internal server error). Here's the log:
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/
The cgi-script doens't depend on the output of the subprocess at the moment, so I don't really get the bad-header warning (and there is no such problem when not launching the subprocess). The beginning of the plot-script looks like this:
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
So far I only did rather simple plot with matplotlib and not as subprocesses, so I would really appreciate your help.
PS: I'm using ubuntu 11.04 and apache2
I suspect that your plot script output stuff to stdout and apache is interpreting that as the headers it is expecting from the cgi.
When you call the script with subprocess, redirect the output to dev null, like this
from subprocess import Popen, STDOUT import os p = Popen(["ls","-l"], stdout=open(os.devnull, "w"), stderr=STDOUT)