diff options
author | Manuel Kaufmann <humitos@gmail.com> | 2012-12-05 20:17:39 (GMT) |
---|---|---|
committer | Manuel Kaufmann <humitos@gmail.com> | 2012-12-05 20:17:39 (GMT) |
commit | e8e4eba18fb6e7b8f851f51cb9e0475c9055c336 (patch) | |
tree | 4eca49fbe4d1702e2628e34f1ab519e970c59d4a | |
parent | 59c39ebdcd4166f59814352b8080bea8c0eccc8e (diff) |
Copy chunks in order
-rw-r--r-- | examples/copy_async.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/examples/copy_async.py b/examples/copy_async.py index 602ef47..9699704 100644 --- a/examples/copy_async.py +++ b/examples/copy_async.py @@ -17,8 +17,7 @@ class FileTransfer(GObject.GObject): self._input_stream = input_stream self._output_stream = output_stream - self._buffer = GLib.Bytes.new([]) - self._buffer = GLib.ByteArray() + self._pending_buffers = [] def start(self): self._input_stream.read_bytes_async( @@ -38,15 +37,18 @@ class FileTransfer(GObject.GObject): print 'Closing the File.' self._input_stream.close(None) else: + self._pending_buffers.append(data) self._input_stream.read_bytes_async( self._CHUNK_SIZE, GLib.PRIORITY_LOW, None, self.__read_async_cb, None) - self._write_next_buffer(data) + self._write_next_buffer() def __write_async_cb(self, output_stream, result, user_data=None): - self._output_stream.write_bytes_finish(result) + size = self._output_stream.write_bytes_finish(result) + print 'Size written:', size - if not self._output_stream.has_pending() and \ + if not self._pending_buffers and \ + not self._output_stream.has_pending() and \ not self._input_stream.has_pending(): output_stream.close(None) print 'DONE' @@ -54,15 +56,16 @@ class FileTransfer(GObject.GObject): else: self._write_next_buffer() - def _write_next_buffer(self, data): + def _write_next_buffer(self): - if not self._output_stream.has_pending(): + if self._pending_buffers and not self._output_stream.has_pending(): + data = self._pending_buffers.pop(0) self._output_stream.write_bytes_async( data, GLib.PRIORITY_LOW, None, self.__write_async_cb, None) -test_file_name = '/home/humitos/test.py' +test_file_name = '/home/humitos/mozilla.pdf' test_input_stream = Gio.File.new_for_path(test_file_name).read(None) PATH = tempfile.mkstemp()[1] print PATH |