StackContext allows applications to maintain threadlocal-like state that follows execution as it moves to other execution contexts.python
an exception handler is a kind of stack-local state and when that stack is suspended and resumed in a new context that state needs to be preserved.闭包
一个栈结构的上下文处理类
异常处理也是一个栈结构的上下文应用app
@contextlib.contextmanager
def die_on_error():
try:
yield
except:
logging.error("exception in asynchronous operation",exc_info=True)
sys.exit(1)
with StackContext(die_on_error):
# Any exception thrown here *or in callback and its desendents*
# will cause the process to exit instead of spinning endlessly
# in the ioloop.
http_client.fetch(url, callback)
ioloop.start()
复制代码
from __future__ import with_statement
import contextlib
import functools
import itertools
import logging
import threading
复制代码
class _State(threading.local):
def __init__(self):
self.contexts = ()
_state = _State()
复制代码
全局上下文保存整个执行程序的上下文(栈)
with StackContext(context) 使程序包裹在 (global_context, context)上执行
执行完成后恢复全局上下文less
class StackContext(object):
def __init__(self, context_factory):
self.context_factory = context_factory
def __enter__(self):
self.old_contexts = _state.contexts
_state.contexts = (self.old_contexts +
((StackContext, self.context_factory),))
try:
self.context = self.context_factory()
self.context.__enter__()
except Exception:
_state.contexts = self.old_contexts
raise
def __exit__(self, type, value, traceback):
try:
return self.context.__exit__(type, value, traceback)
finally:
_state.contexts = self.old_contexts
复制代码
捕获上下文执行中抛出而又未被捕获的异常
做用相似 finally
用于执行在程序抛出异常后记录日志、关闭 socket 这些现场清理工做
若是 exception_handler 中返回 True,代表异常已经被处理,不会再抛出socket
from tornado import ioloop
import tornado.stack_context
import contextlib
ioloop = tornado.ioloop.IOLoop.instance()
@contextlib.contextmanager
def context_without_catch():
print("enter context")
yield
print("exit context")
def exception_handler(type, value, traceback):
print "catch uncaught exception:", type, value, traceback
return True
def main():
with tornado.stack_context.ExceptionStackContext(exception_handler):
with tornado.stack_context.StackContext(context_without_catch):
print 0 / 0
main()
# enter context
# catch uncaught exception: <type 'exceptions.ZeroDivisionError'> integer division or modulo by zero <traceback object at 0x0000000003321FC8>
复制代码
__exit__ 中捕获 with 语句所包裹的程序执行中所抛出的异常,调用注册的 exception_handler 进行处理
exception_handler 返回 True,则异常不会蔓延async
class ExceptionStackContext(object):
def __init__(self, exception_handler):
self.exception_handler = exception_handler
def __enter__(self):
self.old_contexts = _state.contexts
_state.contexts = (self.old_contexts +
((ExceptionStackContext, self.exception_handler),))
def __exit__(self, type, value, traceback):
try:
if type is not None:
return self.exception_handler(type, value, traceback)
finally:
_state.contexts = self.old_contexts
复制代码
临时构造一个空的全局上下文函数
class NullContext(object):
def __enter__(self):
self.old_contexts = _state.contexts
_state.contexts = ()
def __exit__(self, type, value, traceback):
_state.contexts = self.old_contexts
复制代码
之因此进行这样复杂的操做,是为了对某些前面执行环境相同的状况省略前面的构造,节省时间,不然,能够用一行代替:tornado
new_contexts = ([NullContext()] + [cls(arg) for (cls,arg) in contexts])
oop
def wrap(fn):
if fn is None:
return None
def wrapped(callback, contexts, *args, **kwargs):
# 函数实际调用时,上下文环境发生了变化,与`contexts = _state.contexts`已经有所不一样
if (len(_state.contexts) > len(contexts) or
any(a[1] is not b[1]
for a, b in itertools.izip(_state.contexts, contexts))):
# contexts have been removed or changed, so start over
new_contexts = ([NullContext()] +
[cls(arg) for (cls,arg) in contexts])
else:
new_contexts = [cls(arg)
for (cls, arg) in contexts[len(_state.contexts):]]
if len(new_contexts) > 1:
with contextlib.nested(*new_contexts):
callback(*args, **kwargs)
elif new_contexts:
with new_contexts[0]:
callback(*args, **kwargs)
else:
callback(*args, **kwargs)
if getattr(fn, 'stack_context_wrapped', False):
return fn
# 保存上下文环境
contexts = _state.contexts
result = functools.partial(wrapped, fn, contexts)
result.stack_context_wrapped = True
return result
复制代码
author:bigfish
copyright: 许可协议 知识共享署名-非商业性使用 4.0 国际许可协议fetch