Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# Ideally, we would manage async access to stdin/stdout/stderr *without*
# setting them to non-blocking mode, because that can break other processes.
# (See for much more detail.)
# Of course we can call read/write in a separate thread, but then we lose
# cancellation support.
# This file demonstrates a weird hack to make blocking read/write cancellable,
# and thus at least theoretically possible to integrate into Trio as ordinary
# first-class operations.
# I blame @geofft.
# This only works on Linux; possibly it only works on glibc.
import trio
import os
import socket
import errno
bad_socket = socket.socket()
class BlockingReadTimeoutError(Exception):
async def blocking_read_with_timeout(fd, count, timeout):
print("reading from fd", fd)
cancel_requested = False
async def kill_it_after_timeout(new_fd):
await trio.sleep(timeout)
print("breaking the fd")
os.dup2(bad_socket.fileno(), new_fd, inheritable=False)
nonlocal cancel_requested
cancel_requested = True
new_fd = os.dup(fd)
print("working fd is", new_fd)
async with trio.open_nursery() as nursery:
nursery.start_soon(kill_it_after_timeout, new_fd)
data = await trio.run_sync_in_worker_thread(, new_fd, count)
except OSError as exc:
if cancel_requested and exc.errno == errno.ENOTCONN:
# Call was successfully cancelled. In a real version we'd
# integrate properly with trio's cancellation tools; here
# we'll just raise an arbitrary error.
raise BlockingReadTimeoutError from None
print("got", data)
return data
os.close(new_fd), 0, 10, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment