| 53 | |
| 54 | |
| 55 | def drain(pipe, destination, state, stop): |
| 56 | try: |
| 57 | with destination.open("xb", buffering=0) as log: |
| 58 | while True: |
| 59 | data = pipe.read(4096) |
| 60 | if not data: |
| 61 | break |
| 62 | state["seen_bytes"] += len(data) |
| 63 | remaining = LOG_BYTES - state["saved_bytes"] |
| 64 | prefix = memoryview(data)[:max(remaining, 0)] |
| 65 | while prefix: |
| 66 | count = log.write(prefix) |
| 67 | if not count: |
| 68 | raise OSError("short log write") |
| 69 | state["saved_bytes"] += count |
| 70 | prefix = prefix[count:] |
| 71 | if state["seen_bytes"] > LOG_BYTES: |
| 72 | state["overflow"] = True |
| 73 | stop.set() |
| 74 | except (OSError, ValueError) as exc: |
| 75 | state["error"] = str(exc) |
| 76 | stop.set() |
| 77 | finally: |
| 78 | pipe.close() |
| 79 | |
| 80 | |
| 81 | def reap(proc): |