forked from evalstate/fast-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoo.py
More file actions
68 lines (52 loc) · 1.57 KB
/
foo.py
File metadata and controls
68 lines (52 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import asyncio
import os
import sys
def _trigger_blocking_error(stdout_fd: int) -> None:
# Redirect stdout to a nonblocking pipe to trigger BlockingIOError quickly
# without spamming the terminal.
read_fd, write_fd = os.pipe()
os.set_blocking(write_fd, False)
os.dup2(write_fd, stdout_fd)
os.close(write_fd)
chunk = b"x" * 32768
for _ in range(3):
os.write(stdout_fd, chunk)
raise RuntimeError("Did not trigger BlockingIOError after filling pipe buffer")
def main() -> None:
# asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
stdin_fd = sys.stdin.fileno()
stdout_fd = sys.stdout.fileno()
stderr_fd = sys.stderr.fileno()
print(
"before:",
os.get_blocking(stdin_fd),
os.get_blocking(stdout_fd),
os.get_blocking(stderr_fd),
)
loop.add_reader(stdin_fd, lambda: None)
print(
"after add_reader:",
os.get_blocking(stdin_fd),
os.get_blocking(stdout_fd),
os.get_blocking(stderr_fd),
)
loop.remove_reader(stdin_fd)
print(
"after remove_reader:",
os.get_blocking(stdin_fd),
os.get_blocking(stdout_fd),
os.get_blocking(stderr_fd),
)
try:
_trigger_blocking_error(stdout_fd)
except BlockingIOError:
# Ensure traceback is visible even if stderr is non-blocking.
try:
os.set_blocking(stderr_fd, True)
except Exception:
pass
raise
if __name__ == "__main__":
main()