Subprocess.poll () false returns

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 Output:

Not running
2

test2.py:

 time.sleep(30) print "done" 

What's happening? Should this return still work?


Due to conflicting results, here is the full test1.py code:

 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()] 

Updated 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) 

Updated output:

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

+6
source share
2 answers

If Popen () cannot find test2.py, it throws an "No such file or directory" error with errno 2. This error number is returned by poll (). Since you seem to be running this script through wsgi, something is like gulping your stderr, and you don't see the error message:

 $ 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 

Now the problem is probably due to the fact that your current working directory of your script is not installed in the script directory by the front-end server, try printing os.getcwd () to see if it expects what you expect.

+7
source

According to this

Output status 2 indicates a problem with the shell executing the command. Have you tried running test2.py directly in the shell to check for problems with it? As Lee noted, it may happen that the shell cannot find the file you are trying to execute, although there may be another problem that caused it to break.

+1
source

Source: https://habr.com/ru/post/922571/


All Articles