Example #1
0
def register(signum, handler, once_only=False):
    """Register a signal handler.

    :Parameters:
        - `signum`: The signal number to register.
        - `handler`: A callable object that takes one parameter which is the
          signal number that was triggered.
        - `once_only`: If True, will only trigger once, and then disable the
          signal handler.  [kqueue only]. Defaults to False.

    :Exceptions:
        - `coro.SimultaneousError`: Another handler is already registered for
          this signal.
    """

    if UNAME in ('FreeBSD', 'Darwin'):
        # first, turn *off* normal signal handling...
        signal.signal(signum, signal.SIG_IGN)
        # register with kqueue
        flags = coro.EV.ADD
        if once_only:
            flags |= coro.EV.ONESHOT
        k_handler = lambda x: handler(x.ident)
        coro.set_handler((signum, coro.EVFILT.SIGNAL), k_handler, flags)
    elif UNAME == 'Linux':
        coro.signalfd_handler(signum, handler)
    else:
        signal.signal(signum, handler)
Example #2
0
def register (signum, handler, once_only=False):
    """Register a signal handler.

    :Parameters:
        - `signum`: The signal number to register.
        - `handler`: A callable object that takes one parameter which is the
          signal number that was triggered.
        - `once_only`: If True, will only trigger once, and then disable the
          signal handler.  [kqueue only]. Defaults to False.

    :Exceptions:
        - `coro.SimultaneousError`: Another handler is already registered for
          this signal.
    """

    if UNAME in ('FreeBSD', 'Darwin'):
        # first, turn *off* normal signal handling...
        signal.signal (signum, signal.SIG_IGN)
        # register with kqueue
        flags = coro.EV.ADD
        if once_only:
            flags |= coro.EV.ONESHOT
        k_handler = lambda x: handler(x.ident)
        coro.set_handler ((signum, coro.EVFILT.SIGNAL), k_handler, flags)
    elif UNAME == 'Linux':
        coro.signalfd_handler (signum, handler)
    else:
        signal.signal (signum, handler)
Example #3
0
def register (fd, callback, what=NOTE_DELETE, once_only=1):
    # register with kqueue
    # without EV_CLEAR the event triggers repeatedly for a single write
    flags = EV_ADD | EV_CLEAR
    if once_only:
        flags |= EV_ONESHOT

    coro.set_handler ((fd, EVFILT_VNODE), callback, flags, what)
Example #4
0
def register(fd, callback, what=NOTE_DELETE, once_only=1):
    # register with kqueue
    # without EV_CLEAR the event triggers repeatedly for a single write
    flags = EV_ADD | EV_CLEAR
    if once_only:
        flags |= EV_ONESHOT

    coro.set_handler((fd, EVFILT_VNODE), callback, flags, what)
Example #5
0
    def test_fired(self):
        s = coro.tcp_sock()
        s.connect(("127.0.0.1", self.port))
        self._fired_blocker_socket = s
        # We need to somehow schedule two threads to both wake up on kevent at
        # the same time in a particular order.  The first one will call close
        # on the socket of the second one.
        f = open("test_fire", "w")
        coro.set_handler((f.fileno(), coro.EVFILT.VNODE), self._fired_closer, fflags=coro.NOTE.DELETE)

        t2 = coro.spawn(self._fired_blocker)
        # t2.set_max_selfish_acts(1)
        # Yield to allow fired blocker to block.
        coro.yield_slice()
        # Now, cause threads blocked on kevents to get scheduled in a specific
        # order.
        os.unlink("test_fire")
        s.send("force send")
        # Let those threads run.
        coro.yield_slice()
    def test_fired(self):
        s = coro.tcp_sock()
        s.connect(('127.0.0.1', self.port))
        self._fired_blocker_socket = s
        # We need to somehow schedule two threads to both wake up on kevent at
        # the same time in a particular order.  The first one will call close
        # on the socket of the second one.
        f = open('test_fire', 'w')
        coro.set_handler((f.fileno(), coro.EVFILT.VNODE),
                         self._fired_closer,
                         fflags=coro.NOTE.DELETE)

        t2 = coro.spawn(self._fired_blocker)
        #t2.set_max_selfish_acts(1)
        # Yield to allow fired blocker to block.
        coro.yield_slice()
        # Now, cause threads blocked on kevents to get scheduled in a specific
        # order.
        os.unlink('test_fire')
        s.send('force send')
        # Let those threads run.
        coro.yield_slice()