-
-
Notifications
You must be signed in to change notification settings - Fork 350
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
Opening/Reading character device #1395
Comments
A shot in the dark: would FdStream help? https://trio.readthedocs.io/en/stable/reference-hazmat.html#unix-specific-api |
Yeah, Trio doesn't have a pre-existing wrapper for /dev/event so you need
to know a bit about the lower-level Unix primitives, but here it's pretty
simple. In Unix, character devices general are "selectable", meaning that
they're set up to be used with the non-blocking io positives. (Unlike e.g.
regular on-disk files, which is what trio.open_file is mostly targeted at.)
And trio.hazmat.FdStream is already set up to handle Unix "selectable"
files.
So you want to do something like:
def open_events(events_dev_path):
return trio.hazmat.FdStream(os.open(events_dev_path, os.O_RDONLY))
(Note the use of os.open to get raw file descriptor, versus using regular
open which would give us a Python file object. This is to avoid FdStream
and the file object from fighting over who's in charge of closing the file
descriptor.)
I'm assuming here that opening a /dev/event object is effectively
instantaneous. If it can block for some reason then you might want to wrap
the actual open call in a trio.to_thread.run_sync.
…On Sun, Feb 9, 2020, 12:12 Quentin Pradet ***@***.***> wrote:
A shot in the dark: would FdStream help?
https://trio.readthedocs.io/en/stable/reference-hazmat.html#unix-specific-api
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1395?email_source=notifications&email_token=AAEU42C4Q6H3LXGDBIWFOCLRCBPUDA5CNFSM4KSC34GKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELGWXUY#issuecomment-583887827>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEU42CYYFZZCC5YKEO2OTTRCBPUDANCNFSM4KSC34GA>
.
|
Thanks, I wasn't aware of I think it would still be great to have a function which handles |
The combination of os.open() + wrapping in FdStream that is possible with Trio's actual current APIs would only work for some devices and named pipes, not most files on disk (nor stdin, for example). I think exposing a nice name for this combo might become a bit of an attractive nuisance wrt users expecting more from it than it's able to deliver. Closing this in favor of #174 and #219 which discuss the more general problem of wrapping "anything you can open" in a nice API. |
Hello,
I tried reading
/dev/input/event0
with trio. This character device generates a byte stream of events.Only function I could find is
trio.open_file
, but looks like its purpose is actual files.How can I open a character device so I'll get some
trio.abc.ReceiveStream
?Thanks!
The text was updated successfully, but these errors were encountered: