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 is going on? Shouldn't this return "Still running"?
Because of a contradicting result here's 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
You can check return code of the process using check_call() method. In case if process returned non-zero value CalledProcessError will be raised.
Popen Function The function should return a pointer to a stream that may be used to read from or write to the pipe while also creating a pipe between the calling application and the executed command. Immediately after starting, the Popen function returns data, and it does not wait for the subprocess to finish.
Returned value If successful, popen() returns a pointer to an open stream that can be used to read or write to a pipe. If unsuccessful, popen() returns a NULL pointer and sets errno to one of the following values: Error Code. Description.
The poll() method will return None if the process is still running and the wait() method will block until the process exits: Check out the following page: https://docs.python.org/3.4/library/subprocess.html#popen-objects. Popen.poll() Check if child process has terminated. Set and return returncode attribute.
If Popen() cannot find test2.py, it produces the error "No such file or directory", with errno 2. This error number is returned by poll(). Since you seem to be running this script through wsgi, something seems to be 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 issue is probably because your current working directory of your script is not set to the script's directory by the front end server, try printing os.getcwd() to see if it's what you're expecting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With