%PDF- %PDF-
Direktori : /usr/lib64/python2.7/site-packages/tornado/ |
Current File : //usr/lib64/python2.7/site-packages/tornado/stack_context.pyo |
� ��L]c @� s� d Z d d l m Z m Z m Z m Z d d l Z d d l Z d d l m Z d e f d � � YZ d e j f d � � YZ e � Z d e f d � � YZ d e f d � � YZ d e f d � � YZ d � Z d � Z d � Z d � Z d S( s `StackContext` allows applications to maintain threadlocal-like state that follows execution as it moves to other execution contexts. The motivating examples are to eliminate the need for explicit ``async_callback`` wrappers (as in `tornado.web.RequestHandler`), and to allow some additional context to be kept for logging. This is slightly magic, but it's an extension of the idea that 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. `StackContext` shifts the burden of restoring that state from each call site (e.g. wrapping each `.AsyncHTTPClient` callback in ``async_callback``) to the mechanisms that transfer control from one context to another (e.g. `.AsyncHTTPClient` itself, `.IOLoop`, thread pools, etc). Example usage:: @contextlib.contextmanager def die_on_error(): try: yield except Exception: 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 descendants* # will cause the process to exit instead of spinning endlessly # in the ioloop. http_client.fetch(url, callback) ioloop.start() Most applications shouldn't have to work with `StackContext` directly. Here are a few rules of thumb for when it's necessary: * If you're writing an asynchronous library that doesn't rely on a stack_context-aware library like `tornado.ioloop` or `tornado.iostream` (for example, if you're writing a thread pool), use `.stack_context.wrap()` before any asynchronous operations to capture the stack context from where the operation was started. * If you're writing an asynchronous library that has some shared resources (such as a connection pool), create those shared resources within a ``with stack_context.NullContext():`` block. This will prevent ``StackContexts`` from leaking from one request to another. * If you want to write something like an exception handler that will persist across asynchronous calls, create a new `StackContext` (or `ExceptionStackContext`), and make your asynchronous calls in a ``with`` block that references your `StackContext`. i ( t absolute_importt divisiont print_functiont with_statementN( t raise_exc_infot StackContextInconsistentErrorc B� s e Z RS( ( t __name__t __module__( ( ( s; /usr/lib64/python2.7/site-packages/tornado/stack_context.pyR M s t _Statec B� s e Z d � Z RS( c C� s t � d f | _ d S( N( t tuplet Nonet contexts( t self( ( s; /usr/lib64/python2.7/site-packages/tornado/stack_context.pyt __init__R s ( R R R ( ( ( s; /usr/lib64/python2.7/site-packages/tornado/stack_context.pyR Q s t StackContextc B� sD e Z d Z d � Z d � Z d � Z d � Z d � Z d � Z RS( s Establishes the given context as a StackContext that will be transferred. Note that the parameter is a callable that returns a context manager, not the context itself. That is, where for a non-transferable context manager you would say:: with my_context(): StackContext takes the function itself rather than its result:: with StackContext(my_context): The result of ``with StackContext() as cb:`` is a deactivation callback. Run this callback when the StackContext is no longer needed to ensure that it is not propagated any further (note that deactivating a context does not affect any instances of that context that are currently pending). This is an advanced feature and not necessary in most applications. c C� s | | _ g | _ t | _ d S( N( t context_factoryR t Truet active( R R ( ( s; /usr/lib64/python2.7/site-packages/tornado/stack_context.pyR k s c C� s t | _ d S( N( t FalseR ( R ( ( s; /usr/lib64/python2.7/site-packages/tornado/stack_context.pyt _deactivatep s c C� s* | j � } | j j | � | j � d S( N( R R t appendt __enter__( R t context( ( s; /usr/lib64/python2.7/site-packages/tornado/stack_context.pyt entert s c C� s&