def schedule_clean_up_writers():
     for output_fd, output_buffer in buffers.iteritems():
         if not output_buffer:
             try_remove_handler(loop, output_fd)
             close_fd(output_fd)
         else:
             loop.add_handler(output_fd, partial(writer, terminating=True),
                              loop.WRITE | loop.ERROR)
    def writer(fd, events, terminating=False):
        if events & loop.ERROR:
            try_remove_handler(loop, fd)
            del buffers[fd]
            return

        # There's no input -- unschedule the writer, it'll be rescheduled again
        # when there's something for it to write.
        if not buffers[fd]:
            try_remove_handler(loop, fd)
            if terminating:
                close_fd(fd)
            return

        data = buffers[fd].pop()
        while True:
            try:
                os.write(fd, data)
            except (Error.EPIPE, Error.ECONNRESET, Error.EIO, Error.EBADF):
                del buffers[fd]
                try_remove_handler(loop, fd)
            except (Error.EAGAIN, Error.EINTR):
                continue
            break
 def clean_up_reader(input_fd, close=False):
     try_remove_handler(loop, input_fd)
     if close:
         close_fd(input_fd)