Skip to content

Commit

Permalink
update the subprocess startup so as to alleviate the BrokenPipe error
Browse files Browse the repository at this point in the history
  • Loading branch information
shadeofblue committed Oct 11, 2023
1 parent e2541e6 commit 2791c62
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
28 changes: 20 additions & 8 deletions subprocess_child.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import argparse
import tempfile
import time
import sys
import logging

logging.basicConfig(filename='subprocess.log', level=logging.DEBUG)

logger = logging.getLogger(__name__)

_, path = tempfile.mkstemp(prefix="takietam-subprocess-child-")

Expand All @@ -11,15 +17,21 @@

sleep_cnt = args.sleep_count

while sleep_cnt > 0:
try:
while sleep_cnt > 0:

with open(path, "a") as f:
f.write(f"{sleep_cnt}\n")
with open(path, "a") as f:
f.write(f"{sleep_cnt}\n")

print(sleep_cnt)
logger.info(sleep_cnt)
print(sleep_cnt)
sys.stdout.flush()

if args.error_on and args.error_on >= sleep_cnt:
raise Exception("erroring out as instructed ...")
if args.error_on and args.error_on >= sleep_cnt:
raise Exception("erroring out as instructed ...")

sleep_cnt -= 1
time.sleep(1)
sleep_cnt -= 1
time.sleep(1)
except Exception as e:
logger.error(e)
raise
25 changes: 15 additions & 10 deletions subprocess_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import subprocess
import time

OUTFILE = "subprocess.out"
ERRFILE = "subprocess.err"

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--sleep-count", type=int, default=100)
Expand All @@ -21,22 +23,25 @@

print(f"Launching: {cmd}, new_session={bool(args.new_session)}")

proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
start_new_session=bool(args.new_session),
)
with open(OUTFILE, "w") as outfile, open(ERRFILE, "w") as errfile:
proc = subprocess.Popen(
cmd,
stdout=outfile,
stderr=errfile,
start_new_session=bool(args.new_session),
)

launch_count = args.launch_count

while launch_count > 0:
try:
out, err = proc.communicate(timeout=1)
print(
f"exited {launch_count}\n"
f"-- out --\n{out.decode('ascii')}\n-- err --\n{err.decode('ascii')}"
)
with open(OUTFILE, "r") as outfile, open(ERRFILE, "r") as errfile:
print(
f"exited {launch_count}\n"
f"-- out --\n{outfile.read()}\n-- err --\n{errfile.read()}"
f"-- out --\n{str(out)}\n-- err --\n{str(err)}"
)
break
except subprocess.TimeoutExpired:
pass
Expand Down

0 comments on commit 2791c62

Please sign in to comment.