title: Setting timelimit for sftp.get() of Paramiko module
body: <p>I am using Paramiko's SFTP client to download a file from remote server to a client(i.e. get operation)<br />
  The file to be transferred is a bit huge ~1GB.<br />
  So I would like the get operation to timeout if the time is more than 10s.</p>
  <p>But setting the timeout value for connect doesn't work, It appears to be the timeout for just creating the SSH connection and not the timeout for the whole ssh connection.</p>
  <pre><code>ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(host, username=username, password=password, timeout=10.0)
  sftp = ssh.open_sftp()
  start_time = time.time()
  sftp.get(local_path,remote_path)
  elapsed_time = time.time()-start_time
  print elapsed_time
  sftp.close()
  </code></pre>
  <p>I also tried setting the timeout value for the channel but it doesn't work too</p>
  <pre><code>sftp.get_channel.settimeout(10.0)
  </code></pre>
  <p>But this timeout is again just for read/write operations</p>
  <p>There is a similar question <a href="https://stackoverflow.com/questions/9758432/timeout-in-paramiko-python/40221590#40221590">Timeout in paramiko (python)</a> but it only has answer for timeout in creation of SSH-connection</p>
  <hr />
  <p>Update 1: Following the comments of @Martin I implemented a callback function which checks for the time-limit for the get operation of sftp:</p>
  <pre><code>import paramiko
  import time
  
  Class TimeLimitExceeded(Exception):
      pass
  
  timelimit = 10
  start_time = time.time()
  
  def _timer():
      elapsed_time = time.time()-start_time
      if elapsed_time &gt; timelimit:
          raise TimeLimitExceeded
  
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(host, username=username, password=password, timeout=10.0)
  sftp = ssh.open_sftp()
  try:
      sftp.get(local_path,remote_path,_timer)
  except TimeLimitExceeded:
      print &quot;The operation took too much time to complete&quot;
  sftp.close()
  </code></pre>
  <p>But the time to except the exception is a lot, the code is blocking somewhere.
  I dived into Paramiko source code and found the culprit behind it was the <code>_close(self,async=False)</code> method of the <code>sftp_file.py</code>
  Any help to get around this?</p>
  <hr />
  <p>Update 2: Trying to close the channel itself if timelimit is exceeded.
  Then the exception is flushed to console as the <code>prefetch</code> is implemented by separate daemon thread</p>
  <pre class="lang-none prettyprint-override"><code> File &quot;/scratch/divjaisw/python2.7/lib/python2.7/threading.py&quot;, line 801, in __bootstrap_inner
      self.run()
    File &quot;/scratch/divjaisw/python2.7/lib/python2.7/threading.py&quot;, line 754, in run
      self.__target(*self.__args, **self.__kwargs)
    File &quot;/scratch/divjaisw/python_virtual/lib/python2.7/site-packages/paramiko/sftp_file.py&quot;, line 488, in _prefetch_thread
      num = self.sftp._async_request(self, CMD_READ, self.handle, long(offset), int(length))
    File &quot;/scratch/divjaisw/python_virtual/lib/python2.7/site-packages/paramiko/sftp_client.py&quot;, line 754, in _async_request
      self._send_packet(t, msg)
    File &quot;/scratch/divjaisw/python_virtual/lib/python2.7/site-packages/paramiko/sftp.py&quot;, line 170, in _send_packet
      self._write_all(out)
    File &quot;/scratch/divjaisw/python_virtual/lib/python2.7/site-packages/paramiko/sftp.py&quot;, line 133, in _write_all
      n = self.sock.send(out)
    File &quot;/scratch/divjaisw/python_virtual/lib/python2.7/site-packages/paramiko/channel.py&quot;, line 715, in send
      return self._send(s, m)
    File &quot;/scratch/divjaisw/python_virtual/lib/python2.7/site-packages/paramiko/channel.py&quot;, line 1081, in _send
      raise socket.error('Socket is closed')
  error: Socket is closed
  </code></pre>