def _write_piece(self, fd, data, data_len, offset): remaining = data_len - offset if remaining > self.chunk_size: remaining = self.chunk_size piece = data[offset:offset+remaining] aio_write(fd, piece, offset, self._write_callback) return self.event.get()
def _write_piece(self, fd, data, data_len, offset): remaining = data_len - offset if remaining > self.chunk_size: remaining = self.chunk_size piece = data[offset:offset + remaining] event = AsyncResult() callback = partial(self._write_callback, event) aio_write(fd, piece, offset, callback) return event.get()
def _write_piece(self, fd, data, data_len, offset): remaining = data_len - offset if remaining > self.chunk_size: remaining = self.chunk_size piece = data[offset:offset+remaining] event = AsyncResult() callback = partial(self._write_callback, event) aio_write(fd, piece, offset, callback) return event.get()
def write(self, buf, offset=None): """write a buffer object to file""" if not self._write: raise IOError(9, 'Bad file descriptor') if not self._append and self._buffer_size and self._read_buf: # We should clear read cache self._clear_read_buf() if offset is None: offset = self._offset write_size = self._buffer_size if not self._buffer_size and buf: write_size = len(buf) if not self._append and offset != self._offset: self.seek(offset) # Makes sure we write our buffer #If we buffer we use the global buffer if not we use a local buffer if self._buffer_size: lbuf = self._write_buf self._buffer_lock.acquire() if buf: # The a memoryview of the buffer lbuf.extend(buf) # pushed to pyaio so we need to lock else: lbuf = buf while lbuf and len(lbuf) >= self._buffer_size \ or (self._flush and lbuf): result = AsyncResult() def _write_results(rcode, errno): result.set((rcode, errno)) pyaio.aio_write(self._fd, memoryview(lbuf)[0:write_size], offset, _write_results) rcode, errno = result.get() #SLEEP if rcode < 0: # Some kind of error raise IOError(errno, 'AIO Write Error %d' % errno) # Clean up buffer (of actually written bytes) if self._buffer_size: del lbuf[0:rcode] else: lbuf = None self._offset = offset = offset + rcode # Move the file offset if self._buffer_size: self._buffer_lock.release() if buf: return len(buf) else: return 0
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_write(): s = hax() def callback2(rt, er): assert rt == 10 assert er == 0 f = open('/tmp/c.txt', 'r') content = f.read() assert content == "pyaiopyaio" s.x -= 1 fileno = os.open('/tmp/c.txt', os.O_WRONLY | os.O_CREAT | os.O_TRUNC) s.x += 1 ret = pyaio.aio_write(fileno, b"pyaiopyaio", 0, callback2) assert ret == 0 while(s.x != 0): time.sleep(0.05)
def test_aio_write(): s = hax() def callback2(rt, er): assert rt == 10 assert er == 0 f = open('/tmp/c.txt', 'r') content = f.read() assert content == "pyaiopyaio" s.x -= 1 fileno = os.open('/tmp/c.txt', os.O_WRONLY | os.O_CREAT | os.O_TRUNC) s.x += 1 ret = pyaio.aio_write(fileno, b"pyaiopyaio", 0, callback2) assert ret == 0 while (s.x != 0): time.sleep(0.05)