Reputation: 720
I cannot get subprocess.poll() to work in any situation. It keeps complaining:
Traceback (most recent call last):
File "./JobScheduler.py", line 41, in ?
while job1.alive():
File "./JobScheduler.py", line 26, in alive
if self.process.poll() is None:
AttributeError: 'NoneType' object has no attribute 'poll'
Here is my code:
#!/usr/bin/python (using version 2.4)
import sys, subprocess, threading, random, time, logging
class Command(threading.Thread):
def __init__(self, cmd):
super(Command, self).__init__()
self.cmd = cmd
self.process = None
self.jobid = None
self.returncode = None
def run(self):
print "%s: starting job..." % self.getName()
self.process = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0, shell=True)
out, err = self.process.communicate()
self.returncode = self.process.returncode
print "Process complete:"
print " returncode=" + str(self.returncode)
print " output=" + out
def alive(self):
if self.process.poll() is None:
return True
else:
return False
def getJobID(self):
return self.jobid
job1 = Command(cmd)
job1.start()
job2 = Command(cmd)
job2.start()
print "MAIN: Jobs started."
while job1.alive():
print "job still running."
time.sleep(10)
sys.exit(0)
I've tried using poll() in every possible way and simply cannot get it to work. The point where the while() loop is executed the processes are still running.
Sample output:
# ./JobScheduler.py
Thread-1: starting job...
Thread-2: starting job...
MAIN: Jobs started.
Traceback (most recent call last):
File "./JobScheduler.py", line 41, in ?
while job1.alive():
File "./JobScheduler.py", line 26, in alive
if self.process.poll() is None:
AttributeError: 'NoneType' object has no attribute 'poll'
What am I doing wrong here?
Upvotes: 1
Views: 3391
Reputation: 992707
You are calling self.process.poll()
before self.process
has been assigned a value. When you start the thread, it starts by calling your run()
method, but your main code continues to run. You end up calling job1.alive()
before job1.run()
has done any useful work yet.
Upvotes: 4