Reading a file in stdout with twisted

How do we read a file (non-blocking) and print it to standard output (still not blocking)? This is the easiest way I can think of, but it leaves you feeling that there should be a better way. Something that is undergoing some linear modification of LineReceiver - functionality will be even more preferable.

from twisted.internet import stdio, protocol from twisted.protocols.basic import FileSender from twisted.internet import reactor class FileReader(protocol.Protocol): def connectionMade(self): fl = open('myflie.txt', 'rb') d = FileSender().beginFileTransfer(fl, self.transport) d.addBoth(fl.close) d.addBoth(lambda _: reactor.stop()) stdio.StandardIO(FileReader()) reactor.run() 
+7
python asynchronous twisted
source share
1 answer

This is Twisted weakness. Asynchronous file I / O is very difficult to do, and it may not be possible to do the β€œright thing”. There is a ticket that has been open for a long time: https://twistedmatrix.com/trac/ticket/3983 , which may be a useful place to continue this discussion.

The idiom you use there is definitely the closest to what we currently offer.

+3
source share

All Articles