Poll fds with different event masks using python asyncio loop

56 Views Asked by At

Is it possible, in python3.8(.13)/linux, to poll different fds with different event masks using asyncio library, using a poll selector.

I need to monitor for POLLIN event on one fd and POLLPRI | POLLERR on another fd, is it possible?

That's how I'm able to listen on ONE fd:

import asyncio
import selectors
import select

class CustomPollSelector(selectors.PollSelector):
    _selector_cls = select.poll
    _EVENT_READ = select.POLLPRI | select.POLLERR
    _EVENT_WRITE = select.POLLOUT

selector = CustomPollSelector()
event_loop = asyncio.SelectorEventLoop(selector)
.
.
# listen for select.POLLPRI | select.POLLERR events on a fd_1 fd
event_loop.add_reader(fd_1, read_data_cb)
.
.
.
# TODO: how to listen ONLY on POLLIN events on another fd?
.
.
.
event_loop.run_forever()

I can, using a selector, register another fd with appropriate event mask:

selector.register(fd_2, select.POLLIN)

but how to incorporate this registration into the event_loop, as I understand, for each fd there's an assotiation with a handler_cb to call when specified event happens, how to create such an assotiation outside of library code? It looks like not a good idea....

It's strange, but it looks like the asyncio library doesn't provide the full functionality a linux poll/epoll does.

0

There are 0 best solutions below