Skip to content

Instantly share code, notes, and snippets.

@apua
Last active November 28, 2015 08:54
Show Gist options
  • Save apua/2114291d95e9ef4f14a2 to your computer and use it in GitHub Desktop.
Save apua/2114291d95e9ef4f14a2 to your computer and use it in GitHub Desktop.
real world side effect
def check_job_status(job):
if job.status=='error':
raise Exception
def task(...):
"""
It is a task of thread. When it detects job (parent) failed,
it should stop and raise exception so that the thread stop, too.
"""
try:
p = subprocess.Popen(cmd, ...)
while 1:
line = p.stdout.readline()
if (line=='' and p.poll() is not None) or check_job_status(job):
break
# ...
# about 100 lines...
# ...
except:
if 'p' in locals() and p.poll() is not None:
p.terminate()
stdoutdata, stderrdata = p.communicate()
raise Exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment