Skip to content

Commit

Permalink
Speed up the write operation by bulk calling read.
Browse files Browse the repository at this point in the history
Bulk check the ACKs from the server every 32MB
(or every write request). This way you gain speed
but also making sure not to get the error too late
in a large transfer.
This works for smaller files too, since there is a
cleanup routine being called when the file has been transfered.
  • Loading branch information
lndbrg authored and bitprophet committed Mar 1, 2013
1 parent dd7edd8 commit bd1a97a
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions paramiko/sftp_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

from binascii import hexlify
from collections import deque
import socket
import threading
import time
Expand Down Expand Up @@ -54,6 +55,7 @@ def __init__(self, sftp, handle, mode='r', bufsize=-1):
self._prefetch_data = {}
self._prefetch_reads = []
self._saved_exception = None
self._reqs = deque()

def __del__(self):
self._close(async=True)
Expand Down Expand Up @@ -163,12 +165,14 @@ def _read(self, size):
def _write(self, data):
# may write less than requested if it would exceed max packet size
chunk = min(len(data), self.MAX_REQUEST_SIZE)
req = self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), str(data[:chunk]))
if not self.pipelined or self.sftp.sock.recv_ready():
t, msg = self.sftp._read_response(req)
if t != CMD_STATUS:
raise SFTPError('Expected status')
# convert_status already called
self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), str(data[:chunk])))
if not self.pipelined or (len(self._reqs) > 100 and self.sftp.sock.recv_ready()):
while len(self._reqs):
req = self._reqs.popleft()
t, msg = self.sftp._read_response(req)
if t != CMD_STATUS:
raise SFTPError('Expected status')
# convert_status already called
return chunk

def settimeout(self, timeout):
Expand Down

0 comments on commit bd1a97a

Please sign in to comment.