示例#1
0
def create_thread_notifier_pipe(new = True, purge = False):
    """
    Creates a new pipe for the thread notifier.  If new is True, a new pipe
    will always be created; if it is False, it will only be created if one
    already exists.  If purge is True, any previously queued work will be
    discarded.

    This is an internal function, but we export it for kaa.utils.daemonize.
    """
    global _thread_notifier_pipe
    log.info('create thread notifier pipe')

    if not _thread_notifier_pipe and not new:
        return
    elif _thread_notifier_pipe:
        # There is an existing pipe already, so stop monitoring it.
        notifier.socket_remove(_thread_notifier_pipe[0])

    if purge:
        _thread_notifier_lock.acquire()
        del _thread_notifier_queue[:]
        _thread_notifier_lock.release()

    _thread_notifier_pipe = os.pipe()
    fcntl.fcntl(_thread_notifier_pipe[0], fcntl.F_SETFL, os.O_NONBLOCK)
    fcntl.fcntl(_thread_notifier_pipe[1], fcntl.F_SETFL, os.O_NONBLOCK)
    notifier.socket_add(_thread_notifier_pipe[0], _thread_notifier_run_queue)

    if _thread_notifier_queue:
        # A thread is already running and wanted to run something in the
        # mainloop before the mainloop is started. In that case we need
        # to wakeup the loop ASAP to handle the requests.
        os.write(_thread_notifier_pipe[1], "1")
示例#2
0
文件: popen.py 项目: clones/kaa
 def close( self ):
     """
     Close the IO to the child.
     """
     notifier.socket_remove( self.fp )
     self.fp.close()
     if self.logger:
         self.logger.close()
         self.logger = None
示例#3
0
文件: io.py 项目: jpmunz/smartplayer
 def unregister(self):
     """
     Unregister the IOMonitor
     """
     if not self.active:
         return
     if not is_mainthread():
         return MainThreadCallback(self.unregister)()
     notifier.socket_remove(self._id, self._condition-1)
     super(IOMonitor, self).unregister()
示例#4
0
文件: popen.py 项目: clones/kaa
        except IOError, (errno, msg):
            if errno == 11 and not flushing:
                # Resource temporarily unavailable; if we try to read on a
                # non-blocking descriptor we'll get this message.  If we're
                # being called from flush(), it could be because the child
                # process is dead, in which case this errno will occur but
                # we don't want to return True.
                return True
            data = None
        except ValueError:
            # socket already closed
            return False
            
        if not data:
            log.info('No data on %s for pid %s.' % ( self.name, os.getpid()))
            notifier.socket_remove( self.fp )
            self.fp.close()
            if self.logger:
                self.logger.close()
            # FIXME: we know the child is dead.  Rather than wait for it
            # to be reaped we can call __child_died in Process instance.
            return False

        if self.raw_signal.count():
            self.raw_signal.emit(data)

        if self.signal.count() == 0:
            # Nothing connected to the per-line handler, so need to parse.
            # (And if we're dealing with binary data, it may be important
            # _not to parse.)
            return True