6

test1.py:

process = Popen(["python","test2.py"])
time.sleep(3)

alive = process.poll()
if alive is None:
    print "Still running"
else:
    print "Not running\r\n"
    print "%r" % alive

test1.py出力:

Not running
2

test2.py:

time.sleep(30)
print "done"

何が起こっている?これは「まだ実行中」を返すべきではありませんか?


矛盾する結果のため、ここに完全なtest1.pyコードがあります:

import cStringIO
import os
import cgi
import time
from subprocess import Popen

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","test2.py"])
    time.sleep(3)

    alive = process.poll()
    if alive is None:
        print >> output, "Still running"
    else:
        print >> output, "Not running\r\n"
        print >> output, "%r" % alive

    output.write(input.read(int(environ.get('CONTENT_LENGTH', '0'))))
    return [output.getvalue()]

test1.pyを更新しました:

process = Popen(["python","C:/wamp/www/python/popen/test2.py"], shell=True)
    time.sleep(5)

    alive = process.poll()
    if alive is None:
        #print >> output, "%r" % alive
        print >> output, "Still running"
    else:
        print >> output, "Not running"
        print >> output, "%r" % alive
        print >> output, "Current working dir : %s" % os.getcwd()
        print >> output, os.strerror(0)

更新された出力:

Not running
0
Current working dir : C:\wamp\bin\apache\apache2.2.22
No error

4

2 に答える 2

7

Popen() が test2.py を見つけられない場合、「No such file or directory」というエラーが生成され、errno 2 が返されます。このエラー番号は poll() によって返されます。wsgi を介してこのスクリプトを実行しているように見えるため、何かが stderr を飲み込んでいるようで、エラー メッセージは表示されません。

$ cat test1.py
from subprocess import Popen
import time

process = Popen(["python","doesnotexist.py"])
time.sleep(3)

alive = process.poll()
if alive is None:
    print "Still running"
else:
    print "Not running\r\n"
    print "%r" % alive
$ python test1.py 
python: can't open file 'doesnotexist.py': [Errno 2] No such file or directory
Not running

2

問題はおそらく、スクリプトの現在の作業ディレクトリがフロント エンド サーバーによってスクリプトのディレクトリに設定されていないためです。os.getcwd() を印刷して、期待どおりかどうかを確認してください。

于 2012-08-10T12:41:19.947 に答える
1

これによると

終了ステータス2は、コマンドを実行しているシェルに問題があることを示します。シェルで直接実行test2.pyして、問題がないことを確認しましたか?Lieが指摘したように、実行しようとしているファイルがシェルで見つからない可能性がありますが、別の問題が原因でファイルが破損する可能性があります。

于 2012-08-10T12:47:35.890 に答える