| 95 | record["pid"] = process.pid |
| 96 | |
| 97 | def drain(source, destination): |
| 98 | try: |
| 99 | with Path(destination).open("xb") as output: |
| 100 | while True: |
| 101 | block = source.read(65536) |
| 102 | if not block: |
| 103 | break |
| 104 | with lock: |
| 105 | remaining = max(0, min(64 * 1024 * 1024 - state["bytes"], |
| 106 | 512 * 1024 * 1024 - self.total_output_bytes)) |
| 107 | state["bytes"] += len(block) |
| 108 | self.total_output_bytes += len(block) |
| 109 | if len(block) > remaining: |
| 110 | state["overflow"] = True |
| 111 | stop.set() |
| 112 | output.write(block[:remaining]) |
| 113 | except Exception as exc: |
| 114 | with lock: |
| 115 | state["errors"].append(str(exc)) |
| 116 | stop.set() |
| 117 | finally: |
| 118 | source.close() |
| 119 | |
| 120 | threads = [threading.Thread(target=drain, args=(source, record[name]), daemon=True) |
| 121 | for source, name in ((process.stdout, "stdout"), (process.stderr, "stderr"))] |