def read(self, size=0, offset=None): """read a size of bytes from the file, or entire file if 0 """ \ """for speed we assume EOF after first short read""" if not self._read: raise IOError(9, 'Bad file descriptor') if not self._append and self._buffer_size and self._write_buf: self.flush() if offset is None: offset = self._offset if offset != self._offset: self.seek(offset) # To make sure we blow away our read cache if size == 0: # Attempt to read entire file and return in a single return return self._read_file() else: rbuf = bytearray() # Holding Place for multiple reads while len(rbuf) < size: # People get what they ask for # If we don't want to buffer then just read what they want if len(self._read_buf) < size - len(rbuf) and not self._eof: #Ok we are buffer short so lets do a read result = AsyncResult() def _read_results(buf, rcode, errno): result.set((buf, rcode, errno)) read_size = size - len(rbuf) if self._buffer_size: # If we buffer read buffer instead read_size = self._buffer_size pyaio.aio_read(self._fd, offset, read_size, _read_results) buf, rcode, errno = result.get() #SLEEP if rcode < 0: # Some kind of error raise IOError(errno, 'AIO Read Error %d' % errno) #Rcode will be the bytes read so lets push the offset self._offset = offset = offset + rcode if self._buffer_size: self._read_buf.extend(buf) else: rbuf = buf # Pass through because we are not buffering if rcode == 0 or rcode < read_size: # Good Enough self._eof = True #Do a buffer read toread = size - len(rbuf) if self._buffer_size: rbuf.extend(memoryview(self._read_buf)[0:toread]) #Clean up read buffer del self._read_buf[0:toread] if not self._read_buf and self._eof: # Empty buffer and eof break if self._eof and not rbuf: return None #EOF NO DATA else: return rbuf
def test_aio_write_read_stress(): s = hax() def callback2(rt, er): assert rt == 10 assert er == 0 s.x -= 1 def callback(buf, rt, er): if rt > 0: assert len(buf) == rt else: # EOF assert rt == 0 assert er == 0 s.x -= 1 fileno = os.open('/tmp/c.txt', os.O_RDWR | os.O_CREAT | os.O_TRUNC) # These could hit in any order so its not safe to say the for x in range(1000): s.x += 1 ret = pyaio.aio_write(fileno, b"pyaiopyaio", x * 10, callback2) assert ret == 0 time.sleep(0.0001) s.x += 1 ret = pyaio.aio_read(fileno, x * 10, 10, callback) assert ret == 0 time.sleep(0.0001) while(s.x != 0): time.sleep(0.05)
def test_aio_write_read_stress(): s = hax() def callback2(rt, er): assert rt == 10 assert er == 0 s.x -= 1 def callback(buf, rt, er): if rt > 0: assert len(buf) == rt else: # EOF assert rt == 0 assert er == 0 s.x -= 1 fileno = os.open('/tmp/c.txt', os.O_RDWR | os.O_CREAT | os.O_TRUNC) # These could hit in any order so its not safe to say the for x in range(1000): s.x += 1 ret = pyaio.aio_write(fileno, b"pyaiopyaio", x * 10, callback2) assert ret == 0 time.sleep(0.0001) s.x += 1 ret = pyaio.aio_read(fileno, x * 10, 10, callback) assert ret == 0 time.sleep(0.0001) while (s.x != 0): time.sleep(0.05)
def test_aio_read(): s = hax() def callback(buf, rt, er): assert buf == b"#define PY_SSIZE_T_CLEAN" assert rt == len(buf) assert er == 0 s.x -= 1 fileno = os.open('./pyaio/core.c', os.O_RDONLY) s.x += 1 ret = pyaio.aio_read(fileno, 0, 24, callback) assert ret == 0 while(s.x != 0): time.sleep(0.05)
def test_aio_read(): s = hax() def callback(buf, rt, er): assert buf == b"#define PY_SSIZE_T_CLEAN" assert rt == len(buf) assert er == 0 s.x -= 1 fileno = os.open('./pyaio/core.c', os.O_RDONLY) s.x += 1 ret = pyaio.aio_read(fileno, 0, 24, callback) assert ret == 0 while (s.x != 0): time.sleep(0.05)
def _read_piece(self, fd, offset): event = AsyncResult() callback = partial(self._read_callback, event) aio_read(fd, offset, self.chunk_size, callback) return event.get()
def _read_piece(self, fd, offset): aio_read(fd, offset, self.chunk_size, self._read_callback) return self.event.get()