1

Using asyncio's add_reader() method on Unix systems, I can easily monitor file descriptors, such as pipes, for read availability and invoke a callback once the file is available for reading.

For example, if I have two processes connected via a pipe, I'd like to invoke a callback as soon as there is data available in the pipe for reading. The code below works fine on Unix systems:

loop = asyncio.get_running_loop()
loop.add_reader(my_pipe.fileno(), my_callback)

However, since Windows does not support the add_reader method with pipes (only sockets are supported when using the SelectorEventLoop), is there an efficient workaround to achieve the same behavior using only asyncio? If not, what would be a good way to go about it?

3
  • Switch to sockets, for both linux and windows. Commented Dec 9, 2023 at 8:57
  • Are there any downsides to using sockets instead of pipes for inter-process communication, considering that I only need to send simple instructions? Commented Dec 12, 2023 at 19:22
  • I consider implementing a workaround for windows by creating a task that checks, within an executor, whether data is available to be read from the pipe. Poll blocks until there is data available. If data is available, the task could for example set an event for other tasks to process that data. E.g. async def add_reader_windows(my_pipe, data_available): while True: await asyncio.get_running_loop().run_in_executor(None, my_pipe.poll, None) data_available.set() Commented Dec 12, 2023 at 19:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.