Python 有缓冲吗?

Python 有缓冲吗?,python,apache,python-3.x,raspberry-pi,Python,Apache,Python 3.x,Raspberry Pi,我正在尝试在Apache上用Raspberry Pi编写Python脚本。我的脚本包含: #!/usr/bin/env python3 print("Content-type: text/html\n\n") print("<!DOCTYPE html>\n<head>") # ... other code try: with picamera.PiCamera() as camera: camera.capture('camera.jpg')

我正在尝试在Apache上用Raspberry Pi编写Python脚本。我的脚本包含:

#!/usr/bin/env python3
print("Content-type: text/html\n\n")
print("<!DOCTYPE html>\n<head>")

# ... other code

try:
    with picamera.PiCamera() as camera:
       camera.capture('camera.jpg')
except Exception as error:
    errorMsg = error
似乎PiCamera错误输出是通过管道传输到Http输出的。这不是很好吗,因为我的第一个打印函数已经输出了两个空行?错误消息是否表示PiCamera输出被解释为Http头?如果是,原因是什么?

某些Web服务器(即Apache)会缓冲脚本输出,直到脚本结束<代码>sys.stdout.flush()没有帮助,我可以看到


您可以在单独的脚本中取出不可打印的代码,并将其调用为
subprocess.call(['2nd script'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(或
subprocess.DEVNULL

(1)您不需要将代码移动到单独的进程中。捕获异常应该足够了(2)不要使用
子进程。call()
管道一起使用,它可能会挂起子进程,即使用
DEVNULL
放弃输出。如果在print语句之后删除所有代码,会发生什么情况?当
try
块没有写出错误消息时,脚本工作正常,也就是说,即使
try
块导致执行
块,也会按预期生成网页。“*无法打开vchiq instanc”是来自
摄像机的错误消息。捕获
行,因为正在运行的进程不在组
视频
中。当我将www数据添加到group video时,没有超过500个错误,即使由于Pi上没有摄像头而运行了except块,即camera.capture失败并成功捕获。如果将任何内容打印到stderr:
print('error',file=sys.stderr)
,是否会产生500 http错误?如果是这样的话,我添加了
print('error',file=sys.stderr)
,但没有500个错误。虽然我已经解决了我的问题,但我仍然感到困惑。
malformed header from script 'start.py': Bad header: * failed to open vchiq instanc