[英]Is subprocess.Popen working?
test1.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()]
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(在test2.py中),我都得到相同的輸出。 出事了。 我也嘗試讀取輸出,以便至少可以知道它正在讀取文件。 但是我沒有輸出。 這是怎么了
您的第二個腳本(test2.py)僅定義了一個application
函數-它沒有調用該函數,因此在運行腳本時沒有可見的事情發生。
為了擴展@duskwuff已經給出的答案...是的,子進程正在按預期的方式工作。
問題在於test2.py
腳本幾乎可以立即成功完成,因為從命令shell調用該腳本將運行該腳本,但是該腳本沒有入口點。 而且沒有辦法以您正在使用的方式運行它。 test2.py
是一個wsgi應用程序,用於以一種等待連接進入然后通過可調用application
傳遞請求的方式application
。
test2.py
需要實際執行一些操作,可以從命令行執行:
import time
def main():
time.sleep(5)
print "All done"
if __name__ == "__main__":
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.