コード例 #1
0
 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()
コード例 #2
0
ファイル: __init__.py プロジェクト: rymmx-gls/python-slimta
 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()
コード例 #3
0
 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()
コード例 #4
0
    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
コード例 #5
0
ファイル: gevent.py プロジェクト: felipecruz/pyaio
    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
コード例 #6
0
ファイル: test_pyaio.py プロジェクト: felipecruz/pyaio
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)
コード例 #7
0
ファイル: test_pyaio.py プロジェクト: sprig/pyaio
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)
コード例 #8
0
ファイル: test_pyaio.py プロジェクト: felipecruz/pyaio
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)
コード例 #9
0
ファイル: test_pyaio.py プロジェクト: sprig/pyaio
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)