Last active
November 28, 2015 08:54
-
-
Save apua/2114291d95e9ef4f14a2 to your computer and use it in GitHub Desktop.
real world side effect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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