You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@contextlib.contextmanagerdefdie_on_error():
try:
yieldexcept:
logging.error("exception in asynchronous operation",exc_info=True)
sys.exit(1)
withStackContext(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()
head
from __future__ importwith_statementimportcontextlibimportfunctoolsimportitertoolsimportloggingimportthreading
tornado 源码之 StackContext(二)
contents
example usage
head
_State
StackContext
全局上下文保存整个执行程序的上下文(栈)
with StackContext(context) 使程序包裹在 (global_context, context)上执行
执行完成后恢复全局上下文
ExceptionStackContext
捕获上下文执行中抛出而又未被捕获的异常
作用类似 finally
用于执行在程序抛出异常后记录日志、关闭 socket 这些现场清理工作
如果 exception_handler 中返回 True,表明异常已经被处理,不会再抛出
example
__exit__ 中捕获 with 语句所包裹的程序执行中所抛出的异常,调用注册的 exception_handler 进行处理
exception_handler 返回 True,则异常不会蔓延
NullContext
临时构造一个空的全局上下文
wrap
之所以进行这样复杂的操作,是为了对某些前面执行环境相同的情况省略前面的构造,节省时间,否则,可以用一行代替:
new_contexts = ([NullContext()] + [cls(arg) for (cls,arg) in contexts])
copyright
author:bigfish
copyright: 许可协议 知识共享署名-非商业性使用 4.0 国际许可协议
The text was updated successfully, but these errors were encountered: