简体   繁体   中英

Twisted Producer deferred write

In the FTP Server example it uses IWriteFile which expects a consumer the code I have now currently buffers and sends 4mb chunks to a server, however the producer write doesn't seem to expect a deferred but a synchronous write is it safe to use a deferred?

Code

# Deals with giving the FTP Connection a FileConsumer then the File Consumer takes over
class EmailWriter(object):
    implements(IWriteFile)

    def __init__(self, filename):
        print "EmailWriter: %s" % filename
        self.filename = filename
        self._receive = False

    def receive(self):
        assert not self._receive, "Can only call IWriteFile.receive *once* per instance"
        self._receive = True
        # FileConsumer will close the file object
        self.consumer = EmailConsumer("user@gmail.com", "password", "gmail.com", "smtp.gmail.com", 587, self.filename)
        return defer.succeed(self.consumer)

    def close(self):
        # signals that the upload is done
        pass


# Writing Data
class EmailConsumer(object):
    # implements
    # Consumer
    def __init__(self, path, server):
        self.path = path
        self.json_db = JsonDB(path)
        self.server = server

        self.indexes = {}
        self.blocks = 0

        self.start = False
        self.stop = False
        self.producer = None
        self.streaming = None




    def registerProducer(self, producer, streaming):
        # start expecting data
        assert (self.stop == False), "Cannot register multiple times..."
        self.start = True
        self.producer = producer
        self.streaming = streaming


    def unregisterProducer(self):
        # stop expecting data
        self.start = False
        self.stop = True
        self.producer = None
        self.streming = None

    def write(self, bytes):
        # recieve data
        self.buffer += bytes
        if len(self.buffer) > BLOCK_SIZE:
            self.blocks += 1
            d = self.server.send_file(self.buffer)
            d.addCallback(self._done_uploadng, hash(self.buffer), self.blocks)
            self.buffer = ""

    def self._done_uploadng(self, result, hash, block):
        self.index[block] = (hash, self.server.account)
        self.json_db.set_data("index", self.index)

        return result

The short answer is yes, its safe to return a Deferred.

There is also another hint left in docs in twisted.internet.interfaces.IConsumer:

def write(data):
    """
    The producer will write data by calling this method.

    The implementation must be non-blocking and perform whatever
    buffering is necessary.  If the producer has provided enough data
    for now and it is a L{IPushProducer}, the consumer may call its
    C{pauseProducing} method.
    """

So I suggest you wrap the call to self.server.send_file() with self.producer.pauseProducing() and self.producer.resumeProducing(). This way you will not have it called in a re-entrant way, which would happen for a large file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM