测试1.py
import cStringIO
import os
import cgi
import time
import sys
from subprocess import Popen, PIPE, STDOUT
def application(environ, start_response):
headers = []
headers.append(('Content-Type', 'text/plain'))
write = start_response('200 OK', headers)
input = environ['wsgi.input']
output = cStringIO.StringIO()
process = Popen(["python","C:/wamp/www/python/popen/test2.py"], stdout=PIPE, stderr=PIPE)
a = 0
while a < 10:
a += 1
print >> output, "%r" % process.returncode
print >> output, "%r" % process.poll()
print >> output
time.sleep(1)
while True:
out = process.stdout.read(1)
if out == '' and process.poll() != None:
break
if out != '':
sys.stdout.write(out)
sys.stdout.flush()
print >> output, "Output: "+out
output.write(input.read(int(environ.get('CONTENT_LENGTH', '0'))))
return [output.getvalue()]
测试2.py
import cStringIO
import os
import cgi
import time
def application(environ, start_response):
headers = []
headers.append(('Content-Type', 'text/plain'))
write = start_response('200 OK', headers)
input = environ['wsgi.input']
output = cStringIO.StringIO()
time.sleep(15)
print >> output, "done"
output.write(input.read(int(environ.get('CONTENT_LENGTH', '0'))))
return [output.getvalue()]
test1.py 输出
None
None
None
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Output:
无论我设置time.sleep(15)
为 0 秒还是 30 秒(在 test2.py 中),我都会得到相同的输出。出事了。我也尝试读取输出,所以我至少可以告诉它正在读取文件。但是我没有输出。这里出了什么问题?