This is a modification of my previous post. Based on this answer, I use the following for non-blocking read and write method (using Python 2.7):
from __future__ import print_function
from twisted.internet.task import react
from twisted.internet.defer import Deferred
from twisted.internet.fdesc import readFromFD, setNonBlocking
def getFile(filename):
with open(filename) as f:
d = Deferred()
fd = f.fileno()
setNonBlocking(fd)
readFromFD(fd, d.callback)
return d
def main(reactor, filename):
d = getFile(filename)
return d.addCallback(print)
react(main, ['/Users/USER1/Desktop/testfile.txt'])
In fact, I want to store the reading results into a list instead of using print
in return d.addCallback(print)
. I tried the following:
def main(reactor, filename):
d = getFile(filename)
X = []
return d.addCallback(X)
But it seems it is not correct. How can I store the reading results into a list?