-
-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Portable way of waiting for sockets when all you have is a socket descriptor, not a socket object #400
Comments
Yes, that's right. At that level of the API trio is just focused on
exposing the raw OS capabilities, and "wait for arbitrary file descriptor"
is a thing that Unix supports and Windows doesn't. (And even on Unix there
are some funny limitations. Most notably: files on disk will claim to
always be readable/writable.) I wish it were otherwise, but that's
something we'd have to take up with Microsoft about 20 years ago.
What are you trying to do? Maybe there's some alternative?
|
The asynchronous support for psycopg2 requires selecting on the file descriptors, which requires the ability to select on the connection objects returned. |
Oh, I see – so it's only sockets that you want to wait on, but psycopg2 is giving handing you the raw socket descriptors, while You can convert psycopg2's socket descriptors into Python socket objects by using class TrioPgConn:
def __init__(self, ...):
self._conn = psycopg2.connect(..., async=True)
self._sock = socket.fromfd(self._conn.fileno())
async def _wait_ready(self):
# Cribbing from http://initd.org/psycopg/docs/advanced.html#asynchronous-support
while True:
state = self._conn.poll()
if state == psycopg2.extensions.POLL_OK:
return
elif state == psycopg2.extensions.POLL_WRITE:
await trio.hazmat.wait_socket_writable(self._sock)
elif state == psycopg2.extensions.POLL_READ:
await trio.hazmat.wait_socket_readable(self._sock)
else:
raise ... |
Longer term, I wonder if we should relax the type-checking in
So I guess we should probably start allowing socket descriptors in |
I am also interested in this, but for waiting on input events (ex |
@tacaswell If you're interested in Unix specifically, and not sockets, then I think |
I'm writing an async wrapper over psycopg2 for my ORM to work with trio, and I've hit a snag with it - as far as I can see in the docs, there's no universal way of waiting until a file descriptor is readable.
There's
trio.hazmat.wait_readable
, but that only works on *nix, andwait_socket_readable
(as shown in the docs) only works onsocket.socket
objects.I took a quick search of the issues and couldn't find anything related to this either.
The text was updated successfully, but these errors were encountered: