[mod_python] Output from C functions

Gregory Trubetskoy grisha at modpython.org
Sun Oct 29 17:30:43 EST 2000


The cgihandler redirects the sys.stdout to the client. Anything that uses
the real (non python) stdout will just print somewhere just like you
describe.

If you want to do this in C, you should use the req.write function. Here
is what a handler written in C might look like (skipping all the regular
python module stuff. Kids don't try this at home):

#include mod_python.h

static PyObject * handler(PyObject *self, PyObject *args)
{
	PyObject *r;
	requestobject *req = NULL;

	if (!PyArg_ParseTuple(args, "O", &r))
	    return NULL;

	req = (requestobject *)PyObject_GetAttrString(r, "_req");

	ap_send_http_header(req->request_rec);
	req->header_sent = 1;

	ap_table_set(req->request_rec->headers_out, "content-type", 
		     "text/plain");

	PyObject_CallMethod((PyObject *)req, "write", "s", "Hello World!\n");
	
	return PyInt_FromLong(OK);
}


--
  Gregory (Grisha) Trubetskoy
       grisha at modpython.org

On Sun, 29 Oct 2000, Alexis Iglauer wrote:

> I am running a long calculation using a web interface, and am having the
> problem that the web client times out before the results come back.  I fixed
> this by first printing a valid HTTP header and then having the Python script
> print a counter, which the web browser picks up and prints - and then
> doesn't time out.  Works very well.
> 
> BUT - I have now recoded the central calculation routine in C (all the
> control/UI stuff is still Python), and placed a printf routine in there to
> output the iterations like I did in the Python, but it doesn't get to the
> webbrowser.  Any ideas why?  It prints to stdout if I just run the script
> from the console, but not if I call it from apache.  Is there some buffering
> when calling a C routine?
> 
> I am using mod_python 2.6.2 and I wrapped the C function using SWIG.
> 
> Grateful for any pointers.
> 
> Thanks
> Alexis
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python
> 




More information about the Mod_python mailing list