Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is something I wrote as a watchdog for subprocess execution. I use it now a lot, but I'm not so experienced so maybe there are some flaws in it:</p> <pre><code>import subprocess import time def subprocess_execute(command, time_out=60): """executing the command with a watchdog""" # launching the command c = subprocess.Popen(command) # now waiting for the command to complete t = 0 while t &lt; time_out and c.poll() is None: time.sleep(1) # (comment 1) t += 1 # there are two possibilities for the while to have stopped: if c.poll() is None: # in the case the process did not complete, we kill it c.terminate() # and fill the return code with some error value returncode = -1 # (comment 2) else: # in the case the process completed normally returncode = c.poll() return returncode </code></pre> <p>Usage:</p> <pre><code> return = subprocess_execute(['java', '-jar', 'some.jar']) </code></pre> <p>Comments:</p> <ol> <li>here, the watchdog time out is in seconds; but it's easy to change to whatever needed by changing the <code>time.sleep()</code> value. The <code>time_out</code> will have to be documented accordingly;</li> <li>according to what is needed, here it maybe more suitable to raise some exception.</li> </ol> <p>Documentation: I struggled a bit with the documentation of <code>subprocess</code> module to understand that <code>subprocess.Popen</code> is not blocking; the process is executed in parallel (maybe I do not use the correct word here, but I think it's understandable).</p> <p>But as what I wrote is linear in its execution, I really have to wait for the command to complete, with a time out to avoid bugs in the command to pause the nightly execution of the script.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload