Example #1
0
    def _sendfile_cb(self, fut: 'asyncio.Future[None]', out_fd: int,
                     in_fd: int, offset: int, count: int,
                     loop: asyncio.AbstractEventLoop,
                     registered: bool) -> None:
        if registered:
            loop.remove_writer(out_fd)
        if fut.cancelled():
            return

        try:
            n = os.sendfile(out_fd, in_fd, offset, count)
            if n == 0:  # EOF reached
                n = count
        except (BlockingIOError, InterruptedError):
            n = 0
        except Exception as exc:
            set_exception(fut, exc)
            return

        if n < count:
            loop.add_writer(out_fd, self._sendfile_cb, fut, out_fd, in_fd,
                            offset + n, count - n, loop, True)
        else:
            set_result(fut, None)
Example #2
0
async def wait_for_writer(fd: Union[int, io.BufferedWriter], loop: asyncio.AbstractEventLoop) -> None:
    f: asyncio.Future = asyncio.Future()
    loop.add_writer(fd, f.set_result, None)
    f.add_done_callback(lambda f: loop.remove_writer(fd))
    await f