ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: os.fork() different in cgi-script?
by Juha Autero other posts by this author
Jul 2 2003 3:27PM messages near this date
os.fork() different in cgi-script? | Re: os.fork() different in cgi-script?
"Andreas Kuntzagk" <andreas.kuntzagk@[...].de>  writes:

>  So it looks the main() is run 2 times. 
>  Is it so? And if yes, why?

No, it isn't. The problem is that Python uses C STDIO library that has
buffered input and output. The text written in stdout is written in
buffer. Since child process created by fork() is almost identical to
parent process, it will also have a copy of stdout buffer. When
process exits, it closes stdout, which flushes the buffer. So, both
buffers are written to stdout and that causes everything to be printed
twice.

The reason why this doesn happen on command line is that buffers to
terminals are flushed after newline. You can see that by redirecting
script output to file:

$ python script.py > output
$ cat output
Content-Type: text/plain


4309
Content-Type: text/plain


4309

>  I googled for this but only found a similar question from 1997 and no
>  answer.

Which is strange since this common problem and has nothing to do with
Python.
-- 
Juha Autero
http://www.iki.fi/jautero/
Eschew obscurity!


-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
Andreas Kuntzagk
Juha Autero
Andreas Kuntzagk
Andreas Kuntzagk
Michael Coleman
Andreas Kuntzagk
Michael Coleman

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved