當前位置: 首頁>>代碼示例>>Python>>正文


Python signal.pthread_sigmask方法代碼示例

本文整理匯總了Python中signal.pthread_sigmask方法的典型用法代碼示例。如果您正苦於以下問題:Python signal.pthread_sigmask方法的具體用法?Python signal.pthread_sigmask怎麽用?Python signal.pthread_sigmask使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在signal的用法示例。


在下文中一共展示了signal.pthread_sigmask方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _serve

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def _serve(self):
        if hasattr(signal, 'pthread_sigmask'):
            signal.pthread_sigmask(signal.SIG_BLOCK, range(1, signal.NSIG))
        while 1:
            try:
                with self._listener.accept() as conn:
                    msg = conn.recv()
                    if msg is None:
                        break
                    key, destination_pid = msg
                    send, close = self._cache.pop(key)
                    try:
                        send(conn, destination_pid)
                    finally:
                        close()
            except:
                if not util.is_exiting():
                    sys.excepthook(*sys.exc_info()) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:20,代碼來源:resource_sharer.py

示例2: test_sigpending

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def test_sigpending(self):
        code = """if 1:
            import os
            import signal

            def handler(signum, frame):
                1/0

            signum = signal.SIGUSR1
            signal.signal(signum, handler)

            signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
            os.kill(os.getpid(), signum)
            pending = signal.sigpending()
            if pending != {signum}:
                raise Exception('%s != {%s}' % (pending, signum))
            try:
                signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
            except ZeroDivisionError:
                pass
            else:
                raise Exception("ZeroDivisionError not raised")
        """
        assert_python_ok('-c', code) 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:26,代碼來源:test_signal.py

示例3: block_all_signals

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def block_all_signals():
    """Block asynchronous delivery of all signals to this process."""
    signal.pthread_sigmask(signal.SIG_BLOCK, _ALL_SIGNALS) 
開發者ID:sosy-lab,項目名稱:benchexec,代碼行數:5,代碼來源:container.py

示例4: reset_signal_handling

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def reset_signal_handling():
    signal.pthread_sigmask(signal.SIG_SETMASK, {}) 
開發者ID:sosy-lab,項目名稱:benchexec,代碼行數:4,代碼來源:container.py

示例5: test_pending

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def test_pending(self):
        self.check_wakeup("""def test():
            signum1 = signal.SIGUSR1
            signum2 = signal.SIGUSR2

            signal.signal(signum1, handler)
            signal.signal(signum2, handler)

            signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2))
            _testcapi.raise_signal(signum1)
            _testcapi.raise_signal(signum2)
            # Unblocking the 2 signals calls the C signal handler twice
            signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2))
        """,  signal.SIGUSR1, signal.SIGUSR2, ordered=False) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:16,代碼來源:test_signal.py

示例6: test_sigpending

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def test_sigpending(self):
        code = """if 1:
            import os
            import signal

            def handler(signum, frame):
                1/0

            signum = signal.SIGUSR1
            signal.signal(signum, handler)

            signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
            os.kill(os.getpid(), signum)
            pending = signal.sigpending()
            for sig in pending:
                assert isinstance(sig, signal.Signals), repr(pending)
            if pending != {signum}:
                raise Exception('%s != {%s}' % (pending, signum))
            try:
                signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
            except ZeroDivisionError:
                pass
            else:
                raise Exception("ZeroDivisionError not raised")
        """
        assert_python_ok('-c', code) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:28,代碼來源:test_signal.py

示例7: test_sigwait_thread

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def test_sigwait_thread(self):
        # Check that calling sigwait() from a thread doesn't suspend the whole
        # process. A new interpreter is spawned to avoid problems when mixing
        # threads and fork(): only async-safe functions are allowed between
        # fork() and exec().
        assert_python_ok("-c", """if True:
            import os, threading, sys, time, signal

            # the default handler terminates the process
            signum = signal.SIGUSR1

            def kill_later():
                # wait until the main thread is waiting in sigwait()
                time.sleep(1)
                os.kill(os.getpid(), signum)

            # the signal must be blocked by all the threads
            signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
            killer = threading.Thread(target=kill_later)
            killer.start()
            received = signal.sigwait([signum])
            if received != signum:
                print("sigwait() received %s, not %s" % (received, signum),
                      file=sys.stderr)
                sys.exit(1)
            killer.join()
            # unblock the signal, which should have been cleared by sigwait()
            signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
        """) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:31,代碼來源:test_signal.py

示例8: test_pthread_sigmask_arguments

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def test_pthread_sigmask_arguments(self):
        self.assertRaises(TypeError, signal.pthread_sigmask)
        self.assertRaises(TypeError, signal.pthread_sigmask, 1)
        self.assertRaises(TypeError, signal.pthread_sigmask, 1, 2, 3)
        self.assertRaises(OSError, signal.pthread_sigmask, 1700, []) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:7,代碼來源:test_signal.py

示例9: test_pending

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def test_pending(self):
        self.check_wakeup("""def test():
            signum1 = signal.SIGUSR1
            signum2 = signal.SIGUSR2

            signal.signal(signum1, handler)
            signal.signal(signum2, handler)

            signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2))
            os.kill(os.getpid(), signum1)
            os.kill(os.getpid(), signum2)
            # Unblocking the 2 signals calls the C signal handler twice
            signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2))
        """,  signal.SIGUSR1, signal.SIGUSR2, ordered=False) 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:16,代碼來源:test_signal.py

示例10: _extra_main

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def _extra_main(extra_func, threaded, stop_signals, intr_event, proc_idx, args):
    interrupted: Union[threading.Event, mp.synchronize.Event]
    if threaded:
        interrupted = threading.Event()
    else:
        interrupted = mp.Event()
    if not threaded:

        # Since signals only work for the main thread in Python,
        # extra processes in use_threading=True mode should check
        # the intr_event by themselves (probably in their loops).

        def raise_stop(signum, frame):
            if interrupted.is_set():
                pass
            else:
                interrupted.set()
                if signum == signal.SIGINT:
                    raise KeyboardInterrupt
                elif signum == signal.SIGTERM:
                    raise SystemExit
                else:
                    raise InterruptedBySignal(signum)

        # restore signal handler.
        for signum in stop_signals:
            signal.signal(signum, raise_stop)
        signal.pthread_sigmask(signal.SIG_UNBLOCK, stop_signals)
        intr_event = None

    try:
        if not interrupted.is_set():
            extra_func(intr_event, proc_idx, args)
    except (SystemExit, KeyboardInterrupt, InterruptedBySignal):
        log.warning(f'extra_proc[{proc_idx}] did not handle stop signals.')
    finally:
        if not threaded:
            # same as in _worker_main()
            signal.pthread_sigmask(signal.SIG_BLOCK, stop_signals) 
開發者ID:achimnol,項目名稱:aiotools,代碼行數:41,代碼來源:server.py

示例11: check_sigwait

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def check_sigwait(self, wait_func):
        signum = signal.SIGUSR1
        pid = os.getpid()

        old_handler = signal.signal(signum, lambda *args: None)
        self.addCleanup(signal.signal, signum, old_handler)

        code = '\n'.join((
            'import os, time',
            'pid = %s' % os.getpid(),
            'signum = %s' % int(signum),
            'sleep_time = %r' % self.sleep_time,
            'time.sleep(sleep_time)',
            'os.kill(pid, signum)',
        ))

        old_mask = signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
        self.addCleanup(signal.pthread_sigmask, signal.SIG_UNBLOCK, [signum])

        t0 = time.monotonic()
        proc = self.subprocess(code)
        with kill_on_error(proc):
            wait_func(signum)
            dt = time.monotonic() - t0

        self.assertEqual(proc.wait(), 0) 
開發者ID:ShikyoKira,項目名稱:Project-New-Reign---Nemesis-Main,代碼行數:28,代碼來源:eintr_tester.py

示例12: wait_helper

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def wait_helper(self, blocked, test):
        """
        test: body of the "def test(signum):" function.
        blocked: number of the blocked signal
        """
        code = '''if 1:
        import signal
        import sys
        from signal import Signals

        def handler(signum, frame):
            1/0

        %s

        blocked = %s
        signum = signal.SIGALRM

        # child: block and wait the signal
        try:
            signal.signal(signum, handler)
            signal.pthread_sigmask(signal.SIG_BLOCK, [blocked])

            # Do the tests
            test(signum)

            # The handler must not be called on unblock
            try:
                signal.pthread_sigmask(signal.SIG_UNBLOCK, [blocked])
            except ZeroDivisionError:
                print("the signal handler has been called",
                      file=sys.stderr)
                sys.exit(1)
        except BaseException as err:
            print("error: {}".format(err), file=sys.stderr)
            sys.stderr.flush()
            sys.exit(1)
        ''' % (test.strip(), blocked)

        # sig*wait* must be called with the signal blocked: since the current
        # process might have several threads running, use a subprocess to have
        # a single thread.
        assert_python_ok('-c', code) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:45,代碼來源:test_signal.py

示例13: wait_helper

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def wait_helper(self, blocked, test):
        """
        test: body of the "def test(signum):" function.
        blocked: number of the blocked signal
        """
        code = '''if 1:
        import signal
        import sys

        def handler(signum, frame):
            1/0

        %s

        blocked = %s
        signum = signal.SIGALRM

        # child: block and wait the signal
        try:
            signal.signal(signum, handler)
            signal.pthread_sigmask(signal.SIG_BLOCK, [blocked])

            # Do the tests
            test(signum)

            # The handler must not be called on unblock
            try:
                signal.pthread_sigmask(signal.SIG_UNBLOCK, [blocked])
            except ZeroDivisionError:
                print("the signal handler has been called",
                      file=sys.stderr)
                sys.exit(1)
        except BaseException as err:
            print("error: {}".format(err), file=sys.stderr)
            sys.stderr.flush()
            sys.exit(1)
        ''' % (test.strip(), blocked)

        # sig*wait* must be called with the signal blocked: since the current
        # process might have several threads running, use a subprocess to have
        # a single thread.
        assert_python_ok('-c', code) 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:44,代碼來源:test_signal.py

示例14: check_resource_tracker_death

# 需要導入模塊: import signal [as 別名]
# 或者: from signal import pthread_sigmask [as 別名]
def check_resource_tracker_death(self, signum, should_die):
        # bpo-31310: if the semaphore tracker process has died, it should
        # be restarted implicitly.
        from loky.backend.resource_tracker import _resource_tracker
        pid = _resource_tracker._pid
        if pid is not None:
            os.kill(pid, signal.SIGKILL)
            os.waitpid(pid, 0)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            _resource_tracker.ensure_running()
            # in python < 3.3 , the race condition described in bpo-33613 still
            # exists, as this fix requires signal.pthread_sigmask
            time.sleep(1.0)
        pid = _resource_tracker._pid

        os.kill(pid, signum)
        time.sleep(1.0)  # give it time to die

        ctx = get_context("loky")
        with warnings.catch_warnings(record=True) as all_warn:
            warnings.simplefilter("always")

            # remove unrelated MacOS warning messages first
            warnings.filterwarnings(
                "ignore", message='semaphore are broken on OSX')

            sem = ctx.Semaphore()
            sem.acquire()
            sem.release()
            wr = weakref.ref(sem)
            # ensure `sem` gets collected, which triggers communication with
            # the resource_tracker
            del sem
            gc.collect()
            assert wr() is None
            if should_die:
                assert len(all_warn) == 1
                the_warn = all_warn[0]
                assert issubclass(the_warn.category, UserWarning)
                assert "resource_tracker: process died" in str(
                    the_warn.message)
            else:
                assert len(all_warn) == 0 
開發者ID:joblib,項目名稱:loky,代碼行數:46,代碼來源:test_resource_tracker.py


注:本文中的signal.pthread_sigmask方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。