Example #1
0
 def write(self, buf, end=None):
     pos = buf.tell()
     if end is None:
         buf.seek(0, os.SEEK_END)
         end = buf.tell()
         buf.seek(pos)
     limit = end - pos
     if limit == 0:
         return True
     if self.use_sendfile and not isinstance(buf,
                                             (BytesIO, ReadOnlyFileBuffer)):
         try:
             sent = sendfile_to_socket_async(buf, pos, limit, self.socket)
         except CannotSendfile:
             self.use_sendfile = False
             return False
         except SendfileInterrupted:
             return False
         except IOError as e:
             if e.errno in socket_errors_socket_closed:
                 self.ready = self.use_sendfile = False
                 return False
             raise
         finally:
             self.last_activity = monotonic()
         if sent == 0:
             # Something bad happened, was the file modified on disk by
             # another process?
             self.use_sendfile = self.ready = False
             raise IOError(
                 'sendfile() failed to write any bytes to the socket')
     else:
         sent = self.send(buf.read(min(limit, self.send_bufsize)))
     buf.seek(pos + sent)
     return buf.tell() == end
Example #2
0
 def write(self, buf, end=None):
     pos = buf.tell()
     if end is None:
         buf.seek(0, os.SEEK_END)
         end = buf.tell()
         buf.seek(pos)
     limit = end - pos
     if limit == 0:
         return True
     if self.use_sendfile and not isinstance(buf, (BytesIO, ReadOnlyFileBuffer)):
         try:
             sent = sendfile_to_socket_async(buf, pos, limit, self.socket)
         except CannotSendfile:
             self.use_sendfile = False
             return False
         except SendfileInterrupted:
             return False
         except IOError as e:
             if e.errno in socket_errors_socket_closed:
                 self.ready = self.use_sendfile = False
                 return False
             raise
         finally:
             self.last_activity = monotonic()
         if sent == 0:
             # Something bad happened, was the file modified on disk by
             # another process?
             self.use_sendfile = self.ready = False
             raise IOError('sendfile() failed to write any bytes to the socket')
     else:
         sent = self.send(buf.read(min(limit, self.send_bufsize)))
     buf.seek(pos + sent)
     return buf.tell() == end