MCPcopy Create free account
hub / github.com/pytorch/executorch / _execute_subprocess

Function _execute_subprocess

backends/cadence/runtime/executor.py:33–80  ·  view source on GitHub ↗

`subprocess.run(cmd, capture_output=True)` captures stdout/stderr and only returns it at the end. This functions not only does that, but also prints out stdout/stderr non-blockingly when running the command.

(cmd: List[str], cwd: Optional[str] = None)

Source from the content-addressed store, hash-verified

31
32
33def _execute_subprocess(cmd: List[str], cwd: Optional[str] = None) -> Tuple[str, str]:
34 """
35 `subprocess.run(cmd, capture_output=True)` captures stdout/stderr and only
36 returns it at the end. This functions not only does that, but also prints out
37 stdout/stderr non-blockingly when running the command.
38 """
39 logging.debug(f"cmd = \33[33m{cmd}\33[0m, cwd = {cwd}")
40 stdout = ""
41 stderr = ""
42
43 PIPE = subprocess.PIPE
44 with subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE, cwd=cwd) as p:
45 sel = selectors.DefaultSelector()
46 # pyre-fixme[6]: For 1st argument expected `Union[HasFileno, int]` but got
47 # `Optional[IO[bytes]]`.
48 sel.register(p.stdout, selectors.EVENT_READ)
49 # pyre-fixme[6]: For 1st argument expected `Union[HasFileno, int]` but got
50 # `Optional[IO[bytes]]`.
51 sel.register(p.stderr, selectors.EVENT_READ)
52
53 done = False
54 while not done:
55 for key, _ in sel.select():
56 # pyre-fixme[16]: Item `HasFileno` of `Union[HasFileno, int]` has no
57 # attribute `read1`.
58 data = key.fileobj.read1().decode()
59 if not data:
60 done = True
61 break
62
63 if key.fileobj is p.stdout:
64 if not quiet:
65 print(data, end="")
66 stdout += data
67 else:
68 if not quiet:
69 print(data, end="", file=sys.stderr)
70 stderr += data
71
72 # flush stdout and stderr in case there's no newline character at the end
73 # from the subprocess
74 sys.stdout.flush()
75 sys.stderr.flush()
76
77 if p.returncode != 0:
78 raise subprocess.CalledProcessError(p.returncode, p.args, stdout, stderr)
79
80 return stdout, stderr
81
82
83def execute(args: List[str]) -> Tuple[str, str]:

Callers 1

executeFunction · 0.85

Calls 4

debugMethod · 0.80
flushMethod · 0.80
registerMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected