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 Michael Coleman other posts by this author
Jul 2 2003 2:50PM messages near this date
Re: os.fork() different in cgi-script? | splitting a string into 2 new strings
"Andreas Kuntzagk" <andreas.kuntzagk@[...].de>  writes:
>  def main():
>      print "Content-Type: text/plain\n\n"
>      print os.getpid()
>  
>      childPID = os.fork()
>      if childPID == 0:
>          sys.exit()
>      else:
>          os.wait()
>      
>  if __name__ == "__main__":
>      main()
>  
>  when run on a shell, gives the result:
>  ---------------------------
>  Content-Type: text/plain
>  
>  
>  20953
>  ----------------------------
>  
>  but run as a cgi-script it gives:
>  ---------------------------
>  21039
>  Content-Type: text/plain
>  
>  
>  21039
>  ---------------------------
>  So it looks the main() is run 2 times. 
>  Is it so? And if yes, why?

Probably because stdout is being buffered here.  The pid got written in the buffer (but not 
yet actually printed), then the process was forked, then the buffer got flushed (written out
) by each child.

One solution would be to make sure everything is flushed before you fork.

Mike

-- 
Mike Coleman, Scientific Programmer, +1 816 926 4419
Stowers Institute for Biomedical Research
1000 E. 50th St., Kansas City, MO  64110
-- 
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