[Twisted-Python] synchronous problem

Harald Blåtand kozneb at gmail.com
Tue Jul 8 10:49:40 EDT 2008


On Tue, Jul 8, 2008 at 2:07 PM, Wu Zhe <jessewoo at gmail.com> wrote:

> Hi
>
> I have a series of files to transfer, each followed by a line sent to
> specify the file size and crc number. As we can see here I need
> synchronous behavior, only after the previous file transfer complete,
> can the for loop step into the next iteration to send the next file
> via sender.beginFileTransfer. How can i achieve this? Here is the
> problematic code:
>
>    def import_query(self, path_list):
>        number_of_files = len(path_list)
>        line = '3#%s' % number_of_files
>        self.client_instance.sendLine(line)
>
>        sender = FileSender()
>        sender.CHUNK_SIZE = common.chunk_size
>        for path in path_list:
>            f = open(path, 'rb')
>            s = f.read()
>            filesize = len(s)
>            crc = crc32(s)
>            line = '3#%s:%s' % (filesize, crc)
>            self.client_instance.sendLine(line)
>
>            def transfer_completed(lastsent):
>                f.close()
>                self.client_instance.setLineMode()
>
>            # send the file
>            self.setRawMode()
>            d = sender.beginFileTransfer(f, self.client_instance.transport)
>            d.addCallback(transfer_completed)
>


I think I would try this (Python 2.5) -

   @defer.inlineCallbacks
   def import_query(self, path_list):
      ...
      for path in path_list:
         ...
         < delete transfer_complete() >
          ...
         yield sender.beginFileTransfer(f, self.client_instance.transport)
         f.close()
         ...


Harald
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20080708/673762f3/attachment.htm 


More information about the Twisted-Python mailing list