test1.py是subprocess.Popen工作吗?
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()]
test2.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(in test2.py)。有些事情已经结束。此外,我试图读取输出,所以我至少可以告诉它正在读取文件。但我没有输出。这里有什么问题?
当我运行在浏览器中它会等待15秒,返回“完成” test2.py – Rawr 2012-08-10 23:33:39
它看起来像你”重新使用像'mod_wsgi'东西来运行该脚本在你的Web服务器WSGI处理程序。当您通过'python'命令行解释器直接运行脚本时,没有什么等价物发生。 – duskwuff 2012-08-10 23:34:29
哦完美!那么如何让脚本正常运行?我应该在代码的最后放置什么才能使其正常运行? – Rawr 2012-08-10 23:39:30