[Twisted-Python] synchronous problem

Wu Zhe jessewoo at gmail.com
Tue Jul 8 20:56:49 MDT 2008


On Tue, Jul 8, 2008 at 10:49 PM, Harald Blåtand <kozneb at gmail.com> wrote:
>
>
> 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
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>

This is exactly what I want, thank you very much.

-- 
Wu Zhe
Best Regards


More information about the Twisted-Python mailing list