| Server IP : 217.160.0.135 / Your IP : 216.73.217.37 Web Server : Apache System : Linux www 6.18.52-i1-ampere #1203 SMP Mon Sep 14 18:29:59 CEST 2026 aarch64 User : sws1074145052 ( 1074145052) PHP Version : 8.3.32 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /lib/python3/dist-packages/zmq/eventloop/minitornado/__pycache__/ |
Upload File : |
�
mc�co3 � � � d Z ddlmZmZmZmZ ddlZddlZddlm Z G d� de
� � Z G d� d ej � � Z
e
� � Z G d
� de� � Z G d� d
e� � Z G d� de� � Zd� Zd� Zd� Zd� ZdS )a `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`.
� )�absolute_import�division�print_function�with_statementN� )�raise_exc_infoc � � e Zd ZdS )�StackContextInconsistentErrorN)�__name__�
__module__�__qualname__� � �I/usr/lib/python3/dist-packages/zmq/eventloop/minitornado/stack_context.pyr
r
N s � � � � � ��Dr r
c � � e Zd Zd� ZdS )�_Statec �0 � t � � d f| _ d S �N)�tuple�contexts��selfs r �__init__z_State.__init__S s � ����$���
�
�
r N)r r r
r r r r r r R s# � � � � � �(� (� (� (� (r r c �6 � e Zd ZdZd� Zd� Zd� Zd� Zd� Zd� Z dS ) �StackContexta 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 �0 � || _ g | _ d| _ d S �NT)�context_factoryr �active)r r s r r zStackContext.__init__l s � �.�����
�����r c � � d| _ d S �NF�r r s r �_deactivatezStackContext._deactivateq �
� �����r c � � | � � � }| j � |� � |� � � d S r )r r �append� __enter__)r �contexts r �enterzStackContext.enteru sA � ��&�&�(�(���
���W�%�%�%��������r c �f � | j � � � }|� |||� � d S r )r �pop�__exit__)r �type�value� tracebackr( s r �exitzStackContext.exitz s3 � ��-�#�#�%�%������u�i�0�0�0�0�0r c �� � t j | _ | j d | fz | f| _ | j t _ | � � � n# | j t _ � xY w| j S �Nr )�_stater �old_contexts�new_contextsr) r# r s r r'