Python (Twisted) - reading from fifo and sending read data to multiple protocols

I'm trying to write some kind of multi-bot (jabber / irc) that will read messages from a fifo file (basically, one insert), and then send them to the contacts of the irc channel and jabber. So far I have managed to create two factories to connect to jabber and irc, and they seem to work.

However, I have a problem reading the fifo file - I have no idea how to read it in a loop (open the file, read the line, close the file, go to the open file, etc.) outside the reactor loop to get the data that I you need to send, and then get this data in the loop of the reactor to send to both protocols. I was looking for information on how to do this in the best way, but Im completely lost in the dark. Any suggestion / help would be greatly appreciated.

Thanks in advance!

+4
source share
2 answers

You can read / write in a file descriptor without blocking the reactor in the same way as sockets, by the way, sockets do not use file descriptors?

In your case, create a class that implements twisted.internet.interfaces.IReadDescriptor and add to the reactor using twisted.internet.interfaces.IReactorFDSet.addReader . For an example implementation of IReadDescriptor see twisted.internet.tcp.Connection .

I can’t be more specific, because I never did it myself, but I hope this can be a starting point.

+3
source

The fifo problem is the problem. Read from the slot instead. This will be more consistent with the Twisted event-driven model. Trying to do anything outside the control of the reactor is usually the wrong approach.

---- feedback-based update that fifo is an external constraint and not prevented ----

OK, the central problem is that you cannot write code in the main (and only) thread of your Twisted application, which blocks read requests on fifo. This will stop the entire application if there is nothing to read. Thus, you either look at fifo asynchronously, create a separate stream to read it, or split the application into two.

The last option - the easiest - change the Twisted application so that it listens on the socket and writes a separate small redirector application that runs in a simple loop, reads fifo and writes everything it hears to the socket.

+1
source

Source: https://habr.com/ru/post/1304553/


All Articles