askrobots / dbbasic-pipe
Public
Two-way flow control for Unix pipes: a downstream command tells the upstream one how much it needs, like a SQL optimizer.
plimit.py · 2.2 KB · 92 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Limit command - outputs first N lines and signals backpressure. |
| 4 | |
| 5 | Usage: |
| 6 | plimit <count> |
| 7 | |
| 8 | Works standalone or with PIPE_CONTROL_SOCKET coordination. |
| 9 | |
| 10 | Examples: |
| 11 | cat file.txt | plimit 10 |
| 12 | echo "test" | plimit 5 |
| 13 | """ |
| 14 | |
| 15 | import os |
| 16 | import sys |
| 17 | import socket |
| 18 | import json |
| 19 | |
| 20 | |
| 21 | def main(): |
| 22 | if len(sys.argv) < 2: |
| 23 | print("Usage: plimit <count>", file=sys.stderr) |
| 24 | sys.exit(1) |
| 25 | |
| 26 | try: |
| 27 | limit = int(sys.argv[1]) |
| 28 | except ValueError: |
| 29 | print(f"Error: count must be an integer", file=sys.stderr) |
| 30 | sys.exit(1) |
| 31 | |
| 32 | # Check for coordination socket |
| 33 | ctrl_socket = os.getenv('PIPE_CONTROL_SOCKET') |
| 34 | sock = None |
| 35 | |
| 36 | if ctrl_socket and os.path.exists(ctrl_socket): |
| 37 | try: |
| 38 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 39 | sock.connect(ctrl_socket) |
| 40 | |
| 41 | # Register |
| 42 | msg = { |
| 43 | 'type': 'register', |
| 44 | 'pid': os.getpid(), |
| 45 | 'command': 'plimit', |
| 46 | 'value': limit |
| 47 | } |
| 48 | sock.send(json.dumps(msg).encode() + b'\n') |
| 49 | |
| 50 | # Send backpressure signal |
| 51 | msg = { |
| 52 | 'type': 'backpressure', |
| 53 | 'count': limit |
| 54 | } |
| 55 | sock.send(json.dumps(msg).encode() + b'\n') |
| 56 | |
| 57 | print(f"[plimit] Signaled backpressure: {limit}", file=sys.stderr) |
| 58 | |
| 59 | except Exception as e: |
| 60 | print(f"[plimit] Coordination failed: {e}", file=sys.stderr) |
| 61 | sock = None |
| 62 | |
| 63 | # Process data |
| 64 | count = 0 |
| 65 | try: |
| 66 | for line in sys.stdin: |
| 67 | print(line, end='') |
| 68 | count += 1 |
| 69 | if count >= limit: |
| 70 | break |
| 71 | except BrokenPipeError: |
| 72 | # Downstream closed - expected |
| 73 | pass |
| 74 | except KeyboardInterrupt: |
| 75 | pass |
| 76 | finally: |
| 77 | if sock: |
| 78 | # Notify completion |
| 79 | try: |
| 80 | msg = { |
| 81 | 'type': 'complete', |
| 82 | 'pid': os.getpid(), |
| 83 | 'processed': count |
| 84 | } |
| 85 | sock.send(json.dumps(msg).encode() + b'\n') |
| 86 | sock.close() |
| 87 | except: |
| 88 | pass |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | main() |