[Twisted-Python] Stoping a deferedList

Jean-Paul Calderone exarkun at divmod.com
Mon Jan 28 10:06:35 EST 2008


On Mon, 28 Jan 2008 15:29:11 +0100, tarjei <tarjei at nu.no> wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Hi,
>
>I'm using the FtpClient to upload a set of files. I start the uploads
>with the following code:
>
>
>   def fileError(self, result):
>        log.msg("Error has happened" + result.getErrorMessage())
>        log.msg(result.trap(Exception))
>        return result
>
>    def endError(self, result):
>        log.msg("End Error has happened")
>        self.client.quit()
>        self.client.transport.loseConnection()
>        result.trap(Exception)

This trap call is a bit pointless.  Everything is an Exception.

>
>    def connectionMade(self,ftpClient):
>        "Checks if there are more files to upload and uploads them."
>        deffereds = []
>        "We'll need the client to do a disconnect later"
>        self.client = ftpClient
>        " Create defereds for each of the different files to be uploaded"
>        for upfile in self.ftpFiles:
>            store, finish = ftpClient.storeFile(self._getPath(
>upfile.get_name() ) )
>            finish.addCallback(self.end).addErrback(self.fileError)
>            store.addCallback(self.send_file, upfile
>).addErrback(self.fileError)

Here you've started a transfer (at least, I assume that's what send_file
does).  This is the operation you want to abort later.  So you need some
operation-specific API for aborting it.  I'm not sure what's in send_file,
so I dunno what that API might be, but that's the key to doing what you
want.

>            deffereds.append(store)
>            deffereds.append(finish)
>            self.numfiles += 1
>        return
>defer.gatherResults(deffereds).addCallback(self.success).addErrback(self.endError)
>
>
>Now, if an error happens during the upload of one file, then I want to
>stop the other files as well and close the connection. When I try to run
>tests using the above code, trial reports that it still has open
>connections.

When an error happens during the upload of one file, endError will be called
with a Failure that indicates which upload failed.  This will let you invoke
the operation-specific cancelling API for all the _other_ uploads.

Jean-Paul




More information about the Twisted-Python mailing list