Re: how to flush STDOUT before end of script

Stanley Gambarin (gambarin@OpenMarket.com)
Fri, 31 Jan 1997 13:50:48 -0500

Message-Id: <199701311850.NAA24725@u4-138.openmarket.com>
To: Echo <echo@echo.cica.fr>
Subject: Re: how to flush STDOUT before end of script 
In-Reply-To: Your message of "Thu, 30 Jan 1997 02:58:31 +0100."
             <Pine.LNX.3.95.970130024746.3272A-100000@echo.cica.fr> 
Date: Fri, 31 Jan 1997 13:50:48 -0500
From: Stanley Gambarin <gambarin@OpenMarket.com>

> 
> I installed fcgi with apache 1.2b6 with perl ..
>  but I'am quite dispointed because apache wait for the end of the script
> execution before sending the whole page. 
> In this cas I see almost nodifference with a normal cgi script.
> 
> How to force apache to print HTML code to the client in "real time" ?
> 
	The fix below will provide you with desired functionality.  It 
involves a one line fix in DrainReqOutBuf() function.  The short description
of the problem and the solution is as follows:  	
	mod_fastcgi was designed to support for the server-push applications,
which generate some data periodically and send it to the server.   This way
part of the output could have been viewed at a time.  So, mod_fastcgi by 
itself functions as wanted, however, what we did not count on is that the
web server would buffer the connections in a blocking fashion.  It seems that
when bwrite function call is executed, the actual data is not written to the
client's socket and remains until (i guess) either you get enough data
to write or connection is closed.  I will need to take a better look at the
Apache's code.  In the meantime, to provide the desired behavior, we just
flush the buffers (which sends the info to the client).

in DrainReqOutbuf()
...
    if(!reqPtr->connection->aborted) {
        bwrite(reqPtr->connection->client, begin, count);
+	bflush(reqPtr->connection->client);
    }

	Note that the above should not introduce any efficiency problems,
since the DrainReqOutbuf() function is only called when either we have a lot
of data to write to the client (which is not the case here) or when we try
to read the sockets for more information, but there is none (which is the 
case here).  Thus, the call to flush the buffer will occur only if we need it
or when we have nothing else to do.  One should not however do something like
while(1) {sleep(1); putchar('a');} since that will degrade server performance.
					Hope that helps... Stanley.

-- 
*******************************************************************************
* To unsubscribe from the fastcgi-developers mailing list		      *
* 		mailto: fastcgi-developers-request@openmarket.com      	      *
*		with body containing: unsubscribe       		      *
* To request help for using the fastcgi-developers mailing list		      *
* 		mailto: fastcgi-developers-request@openmarket.com      	      *
*		with body containing: help		       		      *
*******************************************************************************

*******************************************************************************
* Stanley Gambarin			Open Market Inc.		      *
* FastCGI (soon-to-be) Guru		245 First St. Cambridge MA 02142      *
*					(617) - 949 - 7487		      *
* mailto:gambarin@openmarket.com					      *
* 				http://acs2.bu.edu:8001/~stanleyg (school)    *
*				mailto:stanleyg@cs.bu.edu		      *
*******************************************************************************