本文整理匯總了Python中signal.getsignal方法的典型用法代碼示例。如果您正苦於以下問題:Python signal.getsignal方法的具體用法?Python signal.getsignal怎麽用?Python signal.getsignal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類signal
的用法示例。
在下文中一共展示了signal.getsignal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __call__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def __call__(self, *args, **kwargs):
"""Trap ``SIGTERM`` and call wrapped function."""
self._caught_signal = None
# Register handler for SIGTERM, then call `self.func`
self.old_signal_handler = signal.getsignal(signal.SIGTERM)
signal.signal(signal.SIGTERM, self.signal_handler)
self.func(*args, **kwargs)
# Restore old signal handler
signal.signal(signal.SIGTERM, self.old_signal_handler)
# Handle any signal caught during execution
if self._caught_signal is not None:
signum, frame = self._caught_signal
if callable(self.old_signal_handler):
self.old_signal_handler(signum, frame)
elif self.old_signal_handler == signal.SIG_DFL:
sys.exit(0)
示例2: testTwoResults
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def testTwoResults(self):
unittest.installHandler()
result = unittest.TestResult()
unittest.registerResult(result)
new_handler = signal.getsignal(signal.SIGINT)
result2 = unittest.TestResult()
unittest.registerResult(result2)
self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)
result3 = unittest.TestResult()
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
try:
test(result)
except KeyboardInterrupt:
self.fail("KeyboardInterrupt not handled")
self.assertTrue(result.shouldStop)
self.assertTrue(result2.shouldStop)
self.assertFalse(result3.shouldStop)
示例3: testHandlerReplacedButCalled
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def testHandlerReplacedButCalled(self):
# If our handler has been replaced (is no longer installed) but is
# called by the *new* handler, then it isn't safe to delay the
# SIGINT and we should immediately delegate to the default handler
unittest.installHandler()
handler = signal.getsignal(signal.SIGINT)
def new_handler(frame, signum):
handler(frame, signum)
signal.signal(signal.SIGINT, new_handler)
try:
pid = os.getpid()
os.kill(pid, signal.SIGINT)
except KeyboardInterrupt:
pass
else:
self.fail("replaced but delegated handler doesn't raise interrupt")
示例4: __enter__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def __enter__(self):
self.interrupted = False
self.released = False
self.original_handler = signal.getsignal(self.sig)
def handler(signum, frame):
self.release()
self.interrupted = True
try:
signal.signal(self.sig, handler)
except ValueError:
# when testing we can't use signal, just ignore
pass
return self
示例5: __call__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def __call__(self, *args, **kwargs):
self._caught_signal = None
# Register handler for SIGTERM, then call `self.func`
self.old_signal_handler = signal.getsignal(signal.SIGTERM)
signal.signal(signal.SIGTERM, self.signal_handler)
self.func(*args, **kwargs)
# Restore old signal handler
signal.signal(signal.SIGTERM, self.old_signal_handler)
# Handle any signal caught during execution
if self._caught_signal is not None:
signum, frame = self._caught_signal
if callable(self.old_signal_handler):
self.old_signal_handler(signum, frame)
elif self.old_signal_handler == signal.SIG_DFL:
sys.exit(0)
示例6: _set_SIGCHLD_handler
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def _set_SIGCHLD_handler():
# Windows doesn't support SIGCHLD handler
if sys.platform == 'win32':
return
# can't set signal in child threads
if not isinstance(threading.current_thread(), threading._MainThread):
return
global _SIGCHLD_handler_set
if _SIGCHLD_handler_set:
return
previous_handler = signal.getsignal(signal.SIGCHLD)
if not callable(previous_handler):
previous_handler = None
def handler(signum, frame):
# This following call uses `waitid` with WNOHANG from C side. Therefore,
# Python can still get and update the process status successfully.
_error_if_any_worker_fails()
if previous_handler is not None:
previous_handler(signum, frame)
signal.signal(signal.SIGCHLD, handler)
_SIGCHLD_handler_set = True
示例7: test_save_signal_state
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def test_save_signal_state(self):
"""Tests that the signal state class works properly.
The _SignalState class must set, save, and restore signals
when needed.
"""
if sys.platform == "win32":
self.skipTest("Windows does not have signals.")
def dummy_handler():
"""Assign dummy handler to an arbitrary signal."""
pass
self.assertNotEqual(signal.getsignal(signal.SIGWINCH), dummy_handler)
signal_state = _SignalState()
signal_state.set(signal.SIGWINCH, dummy_handler)
self.assertEqual(signal.getsignal(signal.SIGWINCH), dummy_handler)
signal_state.restore()
self.assertNotEqual(signal.getsignal(signal.SIGWINCH), dummy_handler)
示例8: test_signal
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def test_signal(self):
"""
Check that signals are restored after using _CursesScreen
"""
if sys.platform == "win32":
self.skipTest("Windows does not have signals.")
def dummy_signal_handler():
"""Dummy previous signal handler."""
pass
outer_state = _SignalState()
self.assertNotEqual(signal.getsignal(signal.SIGWINCH), dummy_signal_handler)
outer_state.set(signal.SIGWINCH, dummy_signal_handler)
self.assertEqual(signal.getsignal(signal.SIGWINCH), dummy_signal_handler)
Screen.wrapper(self.signal_check)
self.assertEqual(signal.getsignal(signal.SIGWINCH), dummy_signal_handler)
outer_state.restore()
self.assertNotEqual(signal.getsignal(signal.SIGWINCH), dummy_signal_handler)
示例9: testSecondInterrupt
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def testSecondInterrupt(self):
# Can't use skipIf decorator because the signal handler may have
# been changed after defining this method.
if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
self.skipTest("test requires SIGINT to not be ignored")
result = unittest.TestResult()
unittest.installHandler()
unittest.registerResult(result)
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
result.breakCaught = True
self.assertTrue(result.shouldStop)
os.kill(pid, signal.SIGINT)
self.fail("Second KeyboardInterrupt not raised")
try:
test(result)
except KeyboardInterrupt:
pass
else:
self.fail("Second KeyboardInterrupt not raised")
self.assertTrue(result.breakCaught)
示例10: testHandlerReplacedButCalled
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def testHandlerReplacedButCalled(self):
# Can't use skipIf decorator because the signal handler may have
# been changed after defining this method.
if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
self.skipTest("test requires SIGINT to not be ignored")
# If our handler has been replaced (is no longer installed) but is
# called by the *new* handler, then it isn't safe to delay the
# SIGINT and we should immediately delegate to the default handler
unittest.installHandler()
handler = signal.getsignal(signal.SIGINT)
def new_handler(frame, signum):
handler(frame, signum)
signal.signal(signal.SIGINT, new_handler)
try:
pid = os.getpid()
os.kill(pid, signal.SIGINT)
except KeyboardInterrupt:
pass
else:
self.fail("replaced but delegated handler doesn't raise interrupt")
示例11: MakeValidSysOuts
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def MakeValidSysOuts():
if not isinstance(sys.stdout, SafeOutput):
sys.stdout = sys.stderr = SafeOutput()
# and for the sake of working around something I can't understand...
# prevent keyboard interrupts from killing IIS
import signal
def noOp(a,b):
# it would be nice to get to the bottom of this, so a warning to
# the debug console can't hurt.
print "WARNING: Ignoring keyboard interrupt from ActiveScripting engine"
# If someone else has already redirected, then assume they know what they are doing!
if signal.getsignal(signal.SIGINT) == signal.default_int_handler:
try:
signal.signal(signal.SIGINT, noOp)
except ValueError:
# Not the main thread - can't do much.
pass
示例12: test_signal_signal
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def test_signal_signal(self):
WORKING_CASES = SUPPORTED_SIGNALS + [6]
WEIRD_CASES = {
6: None,
2: signal.default_int_handler}
for x in WORKING_CASES:
#Ideal handler signature
def a(signum, frame):
return x
ret_val = signal.signal(x, a)
if x not in WEIRD_CASES.keys():
self.assertEqual(ret_val, signal.SIG_DFL)
else:
self.assertEqual(ret_val, WEIRD_CASES[x])
self.assertEqual(a, signal.getsignal(x))
#Strange handler signatures
class KNew(object):
def __call__(self, *args, **kwargs):
pass
a = KNew()
ret_val = signal.signal(signal.SIGBREAK, a)
self.assertEqual(a, signal.getsignal(signal.SIGBREAK))
示例13: __enter__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def __enter__(self):
self.interrupted = False
self.released = False
self.original_handler = signal.getsignal(self.sig)
def handler(signum, frame):
self.release()
self.interrupted = True
print("(Caught interrupt. Terminating when safe.... Press Ctrl-C again to force stop)")
signal.signal(self.sig, handler)
return self
示例14: __call__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def __call__(self, signum, frame):
installed_handler = signal.getsignal(signal.SIGINT)
if installed_handler is not self:
# if we aren't the installed handler, then delegate immediately
# to the default handler
self.default_handler(signum, frame)
if self.called:
self.default_handler(signum, frame)
self.called = True
for result in _results.keys():
result.stop()
示例15: installHandler
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import getsignal [as 別名]
def installHandler():
global _interrupt_handler
if _interrupt_handler is None:
default_handler = signal.getsignal(signal.SIGINT)
_interrupt_handler = _InterruptHandler(default_handler)
signal.signal(signal.SIGINT, _interrupt_handler)