Two-way flow control for Unix pipes: a downstream command tells the upstream one how much it needs, like a SQL optimizer.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-pipe.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-pipe.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-pipe.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add PyPI and license badges to README 52c82fe 1 year, 1 month ago History
plimit.py · 2.2 KB · 92 lines
1#!/usr/bin/env python3
2"""
3Limit command - outputs first N lines and signals backpressure.
4
5Usage:
6 plimit <count>
7
8Works standalone or with PIPE_CONTROL_SOCKET coordination.
9
10Examples:
11 cat file.txt | plimit 10
12 echo "test" | plimit 5
13"""
14
15import os
16import sys
17import socket
18import json
19
20
21def 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
91if __name__ == '__main__':
92 main()