本文整理匯總了Python中atexit._exithandlers方法的典型用法代碼示例。如果您正苦於以下問題:Python atexit._exithandlers方法的具體用法?Python atexit._exithandlers怎麽用?Python atexit._exithandlers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類atexit
的用法示例。
在下文中一共展示了atexit._exithandlers方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_args
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def test_args(self):
# be sure args are handled properly
s = StringIO.StringIO()
sys.stdout = sys.stderr = s
save_handlers = atexit._exithandlers
atexit._exithandlers = []
try:
atexit.register(self.h1)
atexit.register(self.h4)
atexit.register(self.h4, 4, kw="abc")
atexit._run_exitfuncs()
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
atexit._exithandlers = save_handlers
self.assertEqual(s.getvalue(), "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
示例2: test_order
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def test_order(self):
# be sure handlers are executed in reverse order
s = StringIO.StringIO()
sys.stdout = sys.stderr = s
save_handlers = atexit._exithandlers
atexit._exithandlers = []
try:
atexit.register(self.h1)
atexit.register(self.h2)
atexit.register(self.h3)
atexit._run_exitfuncs()
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
atexit._exithandlers = save_handlers
self.assertEqual(s.getvalue(), "h3\nh2\nh1\n")
示例3: test_sys_override
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def test_sys_override(self):
# be sure a preset sys.exitfunc is handled properly
s = StringIO.StringIO()
sys.stdout = sys.stderr = s
save_handlers = atexit._exithandlers
atexit._exithandlers = []
exfunc = sys.exitfunc
sys.exitfunc = self.h1
reload(atexit)
try:
atexit.register(self.h2)
atexit._run_exitfuncs()
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
atexit._exithandlers = save_handlers
sys.exitfunc = exfunc
self.assertEqual(s.getvalue(), "h2\nh1\n")
示例4: test_raise
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def test_raise(self):
# be sure raises are handled properly
s = StringIO.StringIO()
sys.stdout = sys.stderr = s
save_handlers = atexit._exithandlers
atexit._exithandlers = []
try:
atexit.register(self.raise1)
atexit.register(self.raise2)
self.assertRaises(TypeError, atexit._run_exitfuncs)
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
atexit._exithandlers = save_handlers
### helpers
示例5: setUp
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def setUp(self):
self.save_stdout = sys.stdout
self.save_stderr = sys.stderr
self.stream = StringIO.StringIO()
sys.stdout = sys.stderr = self.subst_io = self.stream
self.save_handlers = atexit._exithandlers
atexit._exithandlers = []
示例6: tearDown
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def tearDown(self):
sys.stdout = self.save_stdout
sys.stderr = self.save_stderr
atexit._exithandlers = self.save_handlers
示例7: setUp
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def setUp(self):
s = StringIO.StringIO()
self.save_stdout = sys.stdout
self.save_stderr = sys.stderr
sys.stdout = sys.stderr = self.subst_io = s
self.save_handlers = atexit._exithandlers
atexit._exithandlers = []
示例8: init_error_client
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def init_error_client():
""" Send traceback to neuropoly servers
:return:
"""
if os.getenv('SENTRY_DSN'):
logger.debug('Configuring sentry report')
try:
client = raven.Client(
release=__version__,
processors=(
'raven.processors.RemoveStackLocalsProcessor',
'raven.processors.SanitizePasswordsProcessor'),
)
server_log_handler(client)
traceback_to_server(client)
old_exitfunc = sys.exitfunc
def exitfunc():
sent_something = False
try:
# implementation-specific
import atexit
for handler, args, kw in atexit._exithandlers:
if handler.__module__.startswith("raven."):
sent_something = True
except:
pass
old_exitfunc()
if sent_something:
print("Note: you can opt out of Sentry reporting by editing the file ${SCT_DIR}/bin/sct_launcher and delete the line starting with \"export SENTRY_DSN\"")
sys.exitfunc = exitfunc
except raven.exceptions.InvalidDsn:
# This could happen if sct staff change the dsn
logger.debug('Sentry DSN not valid anymore, not reporting errors')
示例9: shutdown
# 需要導入模塊: import atexit [as 別名]
# 或者: from atexit import _exithandlers [as 別名]
def shutdown(self, force_quit_timeout=0):
"""
Shutdown the queue (after finishing any pending requests).
"""
self.logger.info('Shutting down threadpool')
# Add a shutdown request for every worker
for i in range(len(self.workers)):
self.queue.put(ThreadPool.SHUTDOWN)
# Wait for each thread to terminate
hung_workers = []
for worker in self.workers:
worker.join(0.5)
if worker.isAlive():
hung_workers.append(worker)
zombies = []
for thread_id in self.dying_threads:
if self.thread_exists(thread_id):
zombies.append(thread_id)
if hung_workers or zombies:
self.logger.info("%s workers didn't stop properly, and %s zombies",
len(hung_workers), len(zombies))
if hung_workers:
for worker in hung_workers:
self.kill_worker(worker.thread_id)
self.logger.info('Workers killed forcefully')
if force_quit_timeout:
hung = []
timed_out = False
need_force_quit = bool(zombies)
for workers in self.workers:
if not timed_out and worker.isAlive():
timed_out = True
worker.join(force_quit_timeout)
if worker.isAlive():
print "Worker %s won't die" % worker
need_force_quit = True
if need_force_quit:
import atexit
# Remove the threading atexit callback
for callback in list(atexit._exithandlers):
func = getattr(callback[0], 'im_func', None)
if not func:
continue
globs = getattr(func, 'func_globals', {})
mod = globs.get('__name__')
if mod == 'threading':
atexit._exithandlers.remove(callback)
atexit._run_exitfuncs()
print 'Forcefully exiting process'
os._exit(3)
else:
self.logger.info('All workers eventually killed')
else:
self.logger.info('All workers stopped')