예제 #1
0
 def read_unsigned(fd):
     data = b''
     length = UNSIGNED_STRUCT.size
     while len(data) < length:
         s = nb_read(fd, length - len(data))
         if not s:
             raise EOFError('unexpected EOF')
         data += s
     return UNSIGNED_STRUCT.unpack(data)[0]
예제 #2
0
    def drain(self, p):
        '''Reads the named pipe.'''

        self.logging.info('Started.')
        fd = os.open(p, os.O_RDWR | os.O_NONBLOCK)
        gevent_os.make_nonblocking(fd)

        while self.loop():
            try:
                lines = gevent_os.nb_read(fd, 4096).splitlines()
                if len(lines) == 0:
                    sleep(0.5)
                else:
                    self.consume(lines)
            except OSError:
                pass
예제 #3
0
    def drain(self):
        '''Reads the named pipe.'''

        self.logging.info('Started.')
        fd = os.open(self.path, os.O_RDWR | os.O_NONBLOCK)
        gevent_os.make_nonblocking(fd)

        while self.loop():
            try:
                lines = gevent_os.nb_read(fd, 4096).splitlines()
                if len(lines) == 0:
                    sleep(0.5)
                else:
                    self.consume(lines)
            except OSError:
                pass
예제 #4
0
 def read(self, *args):
     return os.nb_read(*args)
예제 #5
0
    def read_events(self, event_buffer_size=DEFAULT_EVENT_BUFFER_SIZE):
      """
      Reads events from inotify and yields them.
      """

      from gevent import os as gos
      gos.make_nonblocking(self._inotify_fd)

      while True:
        try:
          event_buffer = gos.nb_read(self._inotify_fd, event_buffer_size)
        except OSError as e:
          if e.errno == errno.EINTR:
            continue
        break

      with self._lock:
        event_list = []
        for wd, mask, cookie, name in Inotify._parse_event_buffer(event_buffer):
          if wd == -1:
            continue
          wd_path = self._path_for_wd[wd]
          src_path = absolute_path(os.path.join(wd_path, name))
          inotify_event = InotifyEvent(wd, mask, cookie, name, src_path)

          if inotify_event.is_moved_from:
            self.remember_move_from_event(inotify_event)
          elif inotify_event.is_moved_to:
            move_src_path = self.source_for_move(inotify_event)
            if move_src_path in self._wd_for_path:
              # update old path -> new path
              moved_wd = self._wd_for_path[move_src_path]
              del self._wd_for_path[move_src_path]
              self._wd_for_path[inotify_event.src_path] = moved_wd
              self._path_for_wd[moved_wd] = inotify_event.src_path
            src_path = absolute_path(os.path.join(wd_path, name))
            inotify_event = InotifyEvent(wd, mask, cookie, name, src_path)

          if inotify_event.is_ignored:
          # Clean up book-keeping for deleted watches.
            self._remove_watch_bookkeeping(src_path)
            continue

          event_list.append(inotify_event)

          if self.is_recursive and inotify_event.is_directory and inotify_event.is_create:
            # HACK: We need to traverse the directory path
            # recursively and simulate events for newly
            # created subdirectories/files. This will handle
            # mkdir -p foobar/blah/bar; touch foobar/afile

            # TODO: When a directory from another part of the
            # filesystem is moved into a watched directory, this
            # will not generate events for the directory tree.
            # We need to coalesce IN_MOVED_TO events and those
            # IN_MOVED_TO events which don't pair up with
            # IN_MOVED_FROM events should be marked IN_CREATE
            # instead relative to this directory.
            try:
              self._add_watch(src_path, self._event_mask)
            except OSError:
              continue
            for root, dirnames, filenames in os.walk(src_path):
              for dirname in dirnames:
                try:
                  full_path = absolute_path(os.path.join(root, dirname))
                  wd_dir = self._add_watch(full_path, self._event_mask)
                  event_list.append(InotifyEvent(wd_dir,
                      InotifyConstants.IN_CREATE | InotifyConstants.IN_ISDIR, 0, dirname, full_path))
                except OSError:
                  pass
              for filename in filenames:
                full_path = absolute_path(os.path.join(root, filename))
                wd_parent_dir = self._wd_for_path[absolute_path(os.path.dirname(full_path))]
                event_list.append(InotifyEvent(wd_parent_dir,
                    InotifyConstants.IN_CREATE, 0, filename, full_path))
      return event_list
예제 #6
0
 def read(self, fd, count):
     return os.nb_read(fd, count)
예제 #7
0
파일: test__os.py 프로젝트: BSlience/gevent
 def read(self, *args):
     return os.nb_read(*args)
예제 #8
0
 def reader():
     run.append(1)
     return nb_read(pipe_read_fd, 4096)
예제 #9
0
def non_blocking_read(file_obj, count):
    try:
        from gevent.os import nb_read
        return nb_read(file_obj.fileno(), count if count >= 0 else BUFSIZE)
    except ImportError:
        return retry_loop_on_eagain(file_obj.read, count)
예제 #10
0
def non_blocking_read(file_obj, count):
    try:
        from gevent.os import nb_read
        return nb_read(file_obj.fileno(), count if count >= 0 else BUFSIZE)
    except ImportError:
        return retry_loop_on_eagain(file_obj.read, count)