本文整理汇总了Python中signal.getitimer函数的典型用法代码示例。如果您正苦于以下问题:Python getitimer函数的具体用法?Python getitimer怎么用?Python getitimer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getitimer函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_itimer_prof
def test_itimer_prof(self):
# Issue 3864, unknown if this affects earlier versions of freebsd also
if sys.platform=='freebsd6':
if test_support.verbose:
sys.stderr.write('skipping -- itimer not reliable (does not '
'mix well with threading) on freebsd6\n')
return
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
sys.stdout.write("test_itimer_prof: timeout: likely cause: "
"machine too slow or load too high.\n")
return
# profiling itimer should be (0.0, 0.0) now
self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
# and the handler should have been called
self.assertEqual(self.hndl_called, True)
示例2: test_itimer_prof
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)
for i in xrange(100000000):
if signal.getitimer(self.itimer) == (0.0, 0.0):
break # sig_prof handler stopped this itimer
# profiling itimer should be (0.0, 0.0) now
self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
# and the handler should have been called
self.assertEqual(self.hndl_called, True)
示例3: test_itimer_virtual
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)
for i in xrange(100000000):
if signal.getitimer(self.itimer) == (0.0, 0.0):
break # sig_vtalrm handler stopped this itimer
# virtual itimer should be (0.0, 0.0) now
self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
# and the handler should have been called
self.assertEquals(self.hndl_called, True)
示例4: timeout
def timeout(seconds, exception):
if seconds <= 0:
raise ValueError('Invalid timeout: %s' % seconds)
if threading.currentThread().name != 'MainThread':
raise StateException('Interruptingcow can only be used from the '
'MainThread.')
set_sighandler()
depth = len(timers)
timeleft = signal.getitimer(signal.ITIMER_REAL)[0]
if not timers or timeleft > seconds:
try:
signal.setitimer(signal.ITIMER_REAL, seconds)
timers.append(Timer(time.time() + seconds, exception))
yield
finally:
if len(timers) > depth:
# cancel our timer
signal.setitimer(signal.ITIMER_REAL, 0)
timers.pop()
if timers:
# reinstall the parent timer
timeleft = timers[-1].expiration - time.time()
if timeleft > 0:
signal.setitimer(signal.ITIMER_REAL, timeleft)
else:
# the parent timer has expired, trigger the handler
handler()
else:
# not enough time left on the parent timer
yield
示例5: test_itimer_prof
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)
示例6: test_itimer_virtual
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.time()
while time.time() - 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)
示例7: timing_out_itimer
def timing_out_itimer(seconds):
if seconds is None:
yield
return
with raising_signal(signal.SIGALRM, TimeoutError):
oldval, oldint = signal.getitimer(signal.ITIMER_REAL)
if oldval != 0.0:
raise RuntimeError("ITIMER_REAL already in use")
signal.setitimer(signal.ITIMER_REAL, seconds)
try:
yield
finally:
signal.setitimer(signal.ITIMER_REAL, 0)
示例8: timeout
def timeout(seconds, exception):
if threading.currentThread().name != "MainThread":
raise StateException(
"Interruptingcow can only be used from the " "MainThread."
)
if isinstance(seconds, Quota):
quota = seconds
else:
quota = Quota(float(seconds))
set_sighandler()
seconds = quota.remaining()
depth = len(timers)
parenttimeleft = signal.getitimer(signal.ITIMER_REAL)[0]
if not timers or parenttimeleft > seconds:
try:
quota._start()
timers.append(Timer(time.time() + seconds, exception))
if seconds > 0:
signal.setitimer(signal.ITIMER_REAL, seconds)
yield
else:
handler()
finally:
quota._stop()
if len(timers) > depth:
# cancel our timer
signal.setitimer(signal.ITIMER_REAL, 0)
timers.pop()
if timers:
# reinstall the parent timer
parenttimeleft = timers[-1].expiration - time.time()
if parenttimeleft > 0:
signal.setitimer(signal.ITIMER_REAL, parenttimeleft)
else:
# the parent timer has expired, trigger the handler
handler()
else:
# not enough time left on the parent timer
try:
quota._start()
yield
finally:
quota._stop()