Callback for _system.
(p)
| 93 | |
| 94 | |
| 95 | def _system_body(p): |
| 96 | """Callback for _system.""" |
| 97 | enc = DEFAULT_ENCODING |
| 98 | |
| 99 | def stdout_read(): |
| 100 | for line in read_no_interrupt(p.stdout).splitlines(): |
| 101 | line = line.decode(enc, 'replace') |
| 102 | print(line, file=sys.stdout) |
| 103 | |
| 104 | def stderr_read(): |
| 105 | for line in read_no_interrupt(p.stderr).splitlines(): |
| 106 | line = line.decode(enc, 'replace') |
| 107 | print(line, file=sys.stderr) |
| 108 | |
| 109 | Thread(target=stdout_read).start() |
| 110 | Thread(target=stderr_read).start() |
| 111 | |
| 112 | # Wait to finish for returncode. Unfortunately, Python has a bug where |
| 113 | # wait() isn't interruptible (https://bugs.python.org/issue28168) so poll in |
| 114 | # a loop instead of just doing `return p.wait()`. |
| 115 | while True: |
| 116 | result = p.poll() |
| 117 | if result is None: |
| 118 | time.sleep(0.01) |
| 119 | else: |
| 120 | return result |
| 121 | |
| 122 | |
| 123 | def system(cmd): |