0

Pretty much as the title says, I'm trying to run some execfile calls but I am looking for a timeout option so that if my called script takes over ten seconds to run then it will kill the called script and continue...

The signal library/package is only for UNIX, I'm on windows so I'm a little stuck.

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

# Semi-Sequential (Don't wait for it to finish before moving onto the third script)
subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True)

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TEST.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

Any ideas?

Ryflex
  • 5,559
  • 25
  • 79
  • 148

1 Answers1

4

Something like this should work:

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

# Semi-Sequential (Don't wait for it to finish before moving onto the third script)
p1 = subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True)

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TEST.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

#Do you want to kill the "pythonw", "SUBSCRIPTS/TEST.py", "0" command after the "SUBSCRIPTS/TEST.py" call or do you want to allow the pythonw command to continue running until after the "SUBSCRIPTS/TESTSCRIPT.py"

#you need to put this code depending on where the subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True) #script needs to be killed
currentStatus = p1.poll()
if currentStatus is None: #then it is still running
  try:
    p1.kill() #maybe try os.kill(p1.pid,2) if p1.kill does not work
  except:
    #do something else if process is done running - maybe do nothing?
    pass

# Sequential wait to finish before moving onto the next script
try: 
    execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
    errors.write(traceback.format_exc() + '\n')
    errors.write("\n\n")

#or put the code snippet here if you want to allow the pythonw command to continue running until after the SUBSCRIPTS/TESTSCRIPT.py command

s

Paul
  • 7,155
  • 8
  • 41
  • 40
  • I've reformatted my answer to your clarifications. Note my answer should be viewed as a quick and dirty. Depending on what else is going on in your computations the excellent thread module might help you or some more sophisticated process queueing or message passing of some kind. Note the one minor change I made to your code, p1 = subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True) which allows us to get the process information to poll/kill later if needed – Paul Oct 29 '13 at 19:46
  • Yeah, I thought this would run into a problem with the generic execfile commands. **1)** I need to get tracebacks from the subprocesse popens **2)** Is there a way to switch the execfiles to subprocess but make them wait to complete before going onto the next script? Thanks – Ryflex Oct 29 '13 at 22:45
  • Yes, actually all you need to do is use subprocess.call(["COMMAND","ARGS"]) and it will be a blocking call. To have more control (i.e. to continue without blocking) you use Popen. Of course, you can block with Popen after staring a process by using the communicate method like so: p1 = subprocess.Popen(["grep","dsafkjhdasf","/somefile"]) then p1.communicate() will block until the command is finished. see http://docs.python.org/2/library/subprocess.html for much more information – Paul Oct 30 '13 at 12:08