本文整理匯總了Python中signal.setitimer方法的典型用法代碼示例。如果您正苦於以下問題:Python signal.setitimer方法的具體用法?Python signal.setitimer怎麽用?Python signal.setitimer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類signal
的用法示例。
在下文中一共展示了signal.setitimer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sample
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def sample(self, signum, frame): #pylint: disable=unused-argument
"""Samples current stack and writes result in self._stats.
Args:
signum: Signal that activates handler.
frame: Frame on top of the stack when signal is handled.
"""
stack = []
while frame and frame != self.base_frame:
stack.append((
frame.f_code.co_name,
frame.f_code.co_filename,
frame.f_code.co_firstlineno))
frame = frame.f_back
self._stats[tuple(stack)] += 1
signal.setitimer(signal.ITIMER_PROF, _SAMPLE_INTERVAL)
示例2: sig_vtalrm
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def sig_vtalrm(self, *args):
self.hndl_called = True
if self.hndl_count > 3:
# it shouldn't be here, because it should have been disabled.
raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL "
"timer.")
elif self.hndl_count == 3:
# disable ITIMER_VIRTUAL, this function shouldn't be called anymore
signal.setitimer(signal.ITIMER_VIRTUAL, 0)
if test_support.verbose:
print("last SIGVTALRM handler call")
self.hndl_count += 1
if test_support.verbose:
print("SIGVTALRM handler invoked", args)
示例3: test_itimer_prof
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def test_itimer_prof(self):
self.itimer = signal.ITIMER_PROF
signal.signal(signal.SIGPROF, self.sig_prof)
signal.setitimer(self.itimer, 0.2, 0.2)
start_time = time.time()
while time.time() - start_time < 60.0:
# do some work
_ = pow(12345, 67890, 10000019)
if signal.getitimer(self.itimer) == (0.0, 0.0):
break # sig_prof handler stopped this itimer
else: # Issue 8424
self.skipTest("timeout: likely cause: machine too slow or load too "
"high")
# profiling itimer should be (0.0, 0.0) now
self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0))
# and the handler should have been called
self.assertEqual(self.hndl_called, True)
示例4: run
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def run(self, profiler):
weak_profiler = weakref.proxy(profiler)
handle = functools.partial(self.handle_signal, weak_profiler)
t = self.interval
with deferral() as defer:
prev_handle = signal.signal(signal.SIGPROF, handle)
if prev_handle == signal.SIG_DFL:
# sometimes the process receives SIGPROF although the sampler
# unsets the itimer. If the previous handler was SIG_DFL, the
# process will crash when received SIGPROF. To prevent this
# risk, it makes the process to ignore SIGPROF when it isn't
# running if the previous handler was SIG_DFL.
prev_handle = signal.SIG_IGN
defer(signal.signal, signal.SIGPROF, prev_handle)
prev_itimer = signal.setitimer(signal.ITIMER_PROF, t, t)
defer(signal.setitimer, signal.ITIMER_PROF, *prev_itimer)
yield
示例5: test_itimer_virtual
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def test_itimer_virtual(self):
self.itimer = signal.ITIMER_VIRTUAL
signal.signal(signal.SIGVTALRM, self.sig_vtalrm)
signal.setitimer(self.itimer, 0.3, 0.2)
start_time = time.monotonic()
while time.monotonic() - start_time < 60.0:
# use up some virtual time by doing real work
_ = pow(12345, 67890, 10000019)
if signal.getitimer(self.itimer) == (0.0, 0.0):
break # sig_vtalrm handler stopped this itimer
else: # Issue 8424
self.skipTest("timeout: likely cause: machine too slow or load too "
"high")
# virtual itimer should be (0.0, 0.0) now
self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0))
# and the handler should have been called
self.assertEqual(self.hndl_called, True)
# Issue 3864, unknown if this affects earlier versions of freebsd also
示例6: test_itimer_prof
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def test_itimer_prof(self):
self.itimer = signal.ITIMER_PROF
signal.signal(signal.SIGPROF, self.sig_prof)
signal.setitimer(self.itimer, 0.2, 0.2)
start_time = time.monotonic()
while time.monotonic() - start_time < 60.0:
# do some work
_ = pow(12345, 67890, 10000019)
if signal.getitimer(self.itimer) == (0.0, 0.0):
break # sig_prof handler stopped this itimer
else: # Issue 8424
self.skipTest("timeout: likely cause: machine too slow or load too "
"high")
# profiling itimer should be (0.0, 0.0) now
self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0))
# and the handler should have been called
self.assertEqual(self.hndl_called, True)
示例7: start
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def start(self):
self.last_profile_time = timer()
if self.use_signal:
try:
signal.signal(signal.SIGALRM, self._signal)
# the following tells the system to restart interrupted system calls if they are
# interrupted before any data has been transferred. This avoids many of the problems
# related to signals interrupting system calls, see issue #16
signal.siginterrupt(signal.SIGALRM, False)
except ValueError:
raise NotMainThreadError()
signal.setitimer(signal.ITIMER_REAL, self.interval, 0.0)
else:
sys.setprofile(self._profile)
示例8: set_blocking_signal_threshold
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def set_blocking_signal_threshold(self, seconds, action):
"""Sends a signal if the ioloop is blocked for more than s seconds.
Pass seconds=None to disable. Requires python 2.6 on a unixy
platform.
The action parameter is a python signal handler. Read the
documentation for the python 'signal' module for more information.
If action is None, the process will be killed if it is blocked for
too long.
"""
if not hasattr(signal, "setitimer"):
#logging.error("set_blocking_signal_threshold requires a signal module "
# "with the setitimer method")
ht.logger.error("set_blocking_signal_threshold requires a signal module "
"with the setitimer method")
return
self._blocking_signal_threshold = seconds
if seconds is not None:
signal.signal(signal.SIGALRM,
action if action is not None else signal.SIG_DFL)
示例9: set_blocking_signal_threshold
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def set_blocking_signal_threshold(self, seconds, action):
if not hasattr(signal, "setitimer"):
gen_log.error("set_blocking_signal_threshold requires a signal module "
"with the setitimer method")
return
self._blocking_signal_threshold = seconds
if seconds is not None:
signal.signal(signal.SIGALRM,
action if action is not None else signal.SIG_DFL)
示例10: __enter__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def __enter__(self):
"""Enables statistical profiler."""
signal.signal(signal.SIGPROF, self.sample)
signal.setitimer(signal.ITIMER_PROF, _SAMPLE_INTERVAL)
self._start_time = time.time()
return self
示例11: __exit__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def __exit__(self, exc_type, exc_val, exc_tbf):
"""Disables statistical profiler."""
self.run_time = time.time() - self._start_time
signal.setitimer(signal.ITIMER_PROF, 0)
示例12: __call__
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def __call__(self, *args):
# get the old SIGALRM handler
old = signal.signal(signal.SIGALRM, self.handler)
# set the alarm
signal.setitimer(signal.ITIMER_REAL, self.timeout)
try:
result = self.function(*args)
finally:
# restore existing SIGALRM handler
signal.signal(signal.SIGALRM, old)
signal.setitimer(signal.ITIMER_REAL, 0)
return result
示例13: tearDown
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def tearDown(self):
signal.signal(signal.SIGALRM, self.old_alarm)
if self.itimer is not None: # test_itimer_exc doesn't change this attr
# just ensure that itimer is stopped
signal.setitimer(self.itimer, 0)
示例14: sig_prof
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def sig_prof(self, *args):
self.hndl_called = True
signal.setitimer(signal.ITIMER_PROF, 0)
if test_support.verbose:
print("SIGPROF handler invoked", args)
示例15: test_itimer_exc
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import setitimer [as 別名]
def test_itimer_exc(self):
# XXX I'm assuming -1 is an invalid itimer, but maybe some platform
# defines it ?
self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0)
# Negative times are treated as zero on some platforms.
if 0:
self.assertRaises(signal.ItimerError,
signal.setitimer, signal.ITIMER_REAL, -1)