os.fork()在cgi脚本中有什么不同?

os.fork()在cgi脚本中有什么不同?

问题描述:




以下代码:


#!/ usr / bin / python


import os

import sys

def main():

print" Content-Type:text / plain \\ \\ n \ n"

print os.getpid()


childPID = os.fork()

如果childPID = = 0:

sys.exit()

else:

os.wait()


如果__name__ ==" __ main __":

main()


在shell上运行时,会得到结果:

---------------------------

内容类型:text / plain

20953

----------------------------


但是作为cgi脚本运行它给出:

---------------------------

21039

内容类型:text / plain

21039

-------------- -------------

所以看起来main()运行了2次。

是这样吗?如果是的话,为什么呢?


我用Google搜索了这个问题,但是从1997年开始发现了一个类似的问题而没有

回答。


Ciao,Andreas

Hi,

following code:

#!/usr/bin/python

import os
import sys

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?

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

Ciao, Andreas

对不起,忘了:


这是Python 2.2.1,SuSE 8.1上的apache 1.3.26

Andreas
Sorry, forgot:

this is Python 2.2.1, apache 1.3.26 on SuSE 8.1

Andreas


" Andreas Kuntzagk" <一个************** @ mdc-berlin.de>写道:
"Andreas Kuntzagk" <an**************@mdc-berlin.de> writes:
def main():
print" Content-Type:text / plain\\\
\\\
"
print os.getpid()

childPID = os.fork()
如果childPID == 0:
sys.exit()
否则:
os.wait()

如果__name__ ==" __ main __":
main()

在shell上运行时,给出结果:
--------- ------------------
内容类型:text / plain

20953
-------- --------------------

但是作为cgi脚本运行它给出:
-------- -------------------
21039
Content-Type:text / plain

21039
- -------------------------
所以看起来main()运行了2次。
是这样吗?如果是,为什么?
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?




可能是因为stdout正在这里缓冲。 pid写入缓冲区(但尚未打印),然后进程分叉,然后每个孩子刷新(写出)缓冲区。


一个解决方案是在你分叉前确保所有东西都被刷新。


Mike


-

Mike Coleman,科学程序员,+ 1 816 926 4419

Stowers生物医学研究所

1000 E. 50th St.,Kansas City,MO 64110



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

>

2003年7月2日星期三09:50:03 +0000,Michael Coleman写道:
On Wed, 02 Jul 2003 09:50:03 +0000, Michael Coleman wrote:
可能是因为stdout正在这里缓冲。 pid写在缓冲区中(但实际上还没有打印),然后进程分叉,
然后每个孩子刷新(写出)缓冲区。

一个解决方案是确保在你分叉之前刷新所有东西。
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.




Thanx,就是这样。是否可以/建议使stdout无缓冲?

Andreas



Thanx, that''s it. Is it possible/advisable to make stdout unbuffered?

Andreas