%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib64/python2.7/site-packages/tornado/
Upload File :
Create Path :
Current File : //usr/lib64/python2.7/site-packages/tornado/concurrent.pyo

�
��L]c@�s�dZddlmZmZmZmZddlZddlZddlZddl	Z	ddl
mZddlm
Z
mZddlmZmZyddlmZWnek
r�dZnXej�dko�e	jdkZdefd��YZd
efd��YZdefd��YZeZedkr@eZnejefZd�Z defd��YZ!e!�Z"d�Z#e�Z$d�Z%d�Z&dS(s�Utilities for working with threads and ``Futures``.

``Futures`` are a pattern for concurrent programming introduced in
Python 3.2 in the `concurrent.futures` package (this package has also
been backported to older versions of Python and can be installed with
``pip install futures``).  Tornado will use `concurrent.futures.Future` if
it is available; otherwise it will use a compatible class defined in this
module.
i(tabsolute_importtdivisiontprint_functiontwith_statementN(tapp_log(tExceptionStackContexttwrap(traise_exc_infotArgReplacer(tfuturestCPythoniitReturnValueIgnoredErrorcB�seZRS((t__name__t
__module__(((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR/st_TracebackLoggercB�s8eZdZdZd�Zd�Zd�Zd�ZRS(s
Helper to log a traceback upon destruction if not cleared.

    This solves a nasty problem with Futures and Tasks that have an
    exception set: if nobody asks for the exception, the exception is
    never logged.  This violates the Zen of Python: 'Errors should
    never pass silently.  Unless explicitly silenced.'

    However, we don't want to log the exception as soon as
    set_exception() is called: if the calling code is written
    properly, it will get the exception and handle it properly.  But
    we *do* want to log it if result() or exception() was never called
    -- otherwise developers waste a lot of time wondering why their
    buggy code fails silently.

    An earlier attempt added a __del__() method to the Future class
    itself, but this backfired because the presence of __del__()
    prevents garbage collection from breaking cycles.  A way out of
    this catch-22 is to avoid having a __del__() method on the Future
    class itself, but instead to have a reference to a helper object
    with a __del__() method that logs the traceback, where we ensure
    that the helper object doesn't participate in cycles, and only the
    Future has a reference to it.

    The helper object is added when set_exception() is called.  When
    the Future is collected, and the helper is present, the helper
    object is also collected, and its __del__() method will log the
    traceback.  When the Future's result() or exception() method is
    called (and a helper object is present), it removes the the helper
    object, after calling its clear() method to prevent it from
    logging.

    One downside is that we do a fair amount of work to extract the
    traceback from the exception, even when it is never logged.  It
    would seem cheaper to just store the exception object, but that
    references the traceback, which references stack frames, which may
    reference the Future, which references the _TracebackLogger, and
    then the _TracebackLogger would be included in a cycle, which is
    what we're trying to avoid!  As an optimization, we don't
    immediately format the exception; we only do the work when
    activate() is called, which call is delayed until after all the
    Future's callbacks have run.  Since usually a Future has at least
    one callback (typically set by 'yield From') and usually that
    callback extracts the callback, thereby removing the need to
    format the exception.

    PS. I don't claim credit for this solution.  I first heard of it
    in a discussion about closing files when they are collected.
    texc_infotformatted_tbcC�s||_d|_dS(N(RtNoneR(tselfR((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt__init__js	cC�s7|j}|dk	r3d|_tj|�|_ndS(N(RRt	tracebacktformat_exceptionR(RR((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytactivatens		cC�sd|_d|_dS(N(RRR(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytclearts	cC�s2|jr.tjddj|j�j��ndS(Ns(Future exception was never retrieved: %st(RRterrortjointrstrip(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt__del__xs		(sexc_infosformatted_tb(RR
t__doc__t	__slots__RRRR(((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR6s0			tFuturecB�s�eZdZd�Zd�Zd�Zd�Zd�Zd�Zdd�Z
dd�Zd	�Zd
�Z
d�Zd�Zd
�Zd�Zd�Zer�d�ZnRS(sPlaceholder for an asynchronous result.

    A ``Future`` encapsulates the result of an asynchronous
    operation.  In synchronous applications ``Futures`` are used
    to wait for the result from a thread or process pool; in
    Tornado they are normally used with `.IOLoop.add_future` or by
    yielding them in a `.gen.coroutine`.

    `tornado.concurrent.Future` is similar to
    `concurrent.futures.Future`, but not thread-safe (and therefore
    faster for use with single-threaded event loops).

    In addition to ``exception`` and ``set_exception``, methods ``exc_info``
    and ``set_exc_info`` are supported to capture tracebacks in Python 2.
    The traceback is automatically available in Python 3, but in the
    Python 2 futures backport this information is discarded.
    This functionality was previously available in a separate class
    ``TracebackFuture``, which is now a deprecated alias for this class.

    .. versionchanged:: 4.0
       `tornado.concurrent.Future` is always a thread-unsafe ``Future``
       with support for the ``exc_info`` methods.  Previously it would
       be an alias for the thread-safe `concurrent.futures.Future`
       if that package was available and fall back to the thread-unsafe
       implementation if it was not.

    .. versionchanged:: 4.1
       If a `.Future` contains an error but that error is never observed
       (by calling ``result()``, ``exception()``, or ``exc_info()``),
       a stack trace will be logged when the `.Future` is garbage collected.
       This normally indicates an error in the application, but in cases
       where it results in undesired logging it may be necessary to
       suppress the logging by ensuring that the exception is observed:
       ``f.add_done_callback(lambda f: f.exception())``.
    cC�s:t|_d|_d|_t|_d|_g|_dS(N(tFalset_doneRt_resultt	_exc_infot_log_tracebackt
_tb_loggert
_callbacks(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR�s					cC�stS(s�Cancel the operation, if possible.

        Tornado ``Futures`` do not support cancellation, so this method always
        returns False.
        (R (R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytcancel�scC�stS(s�Returns True if the operation has been cancelled.

        Tornado ``Futures`` do not support cancellation, so this method
        always returns False.
        (R (R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt	cancelled�scC�s|jS(s4Returns True if this operation is currently running.(R!(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytrunning�scC�s|jS(s0Returns True if the future has finished running.(R!(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytdone�scC�s5t|_|jdk	r1|jj�d|_ndS(N(R R$R%RR(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt
_clear_tb_log�s	
cC�sP|j�|jdk	r |jS|jdk	r?t|j�n|j�|jS(s8If the operation succeeded, return its result.  If it failed,
        re-raise its exception.

        This method takes a ``timeout`` argument for compatibility with
        `concurrent.futures.Future` but it is an error to call it
        before the `Future` is done, so the ``timeout`` is never used.
        N(R+R"RR#Rt_check_done(Rttimeout((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytresult�s

cC�s6|j�|jdk	r$|jdS|j�dSdS(s@If the operation raised an exception, return the `Exception`
        object.  Otherwise returns None.

        This method takes a ``timeout`` argument for compatibility with
        `concurrent.futures.Future` but it is an error to call it
        before the `Future` is done, so the ``timeout`` is never used.
        iN(R+R#RR,(RR-((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt	exception�s


cC�s*|jr||�n|jj|�dS(s.Attaches the given callback to the `Future`.

        It will be invoked with the `Future` as its argument when the Future
        has finished running and its result is available.  In Tornado
        consider using `.IOLoop.add_future` instead of calling
        `add_done_callback` directly.
        N(R!R&tappend(Rtfn((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytadd_done_callback�s	
cC�s||_|j�dS(s�Sets the result of a ``Future``.

        It is undefined to call any of the ``set`` methods more than once
        on the same object.
        N(R"t	_set_done(RR.((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt
set_result�s	cC�s)|j|j|t|dd�f�dS(s#Sets the exception of a ``Future.``t
__traceback__N(tset_exc_infot	__class__tgetattrR(RR/((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt
set_exception�scC�s|j�|jS(seReturns a tuple in the same format as `sys.exc_info` or None.

        .. versionadded:: 4.0
        (R+R#(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyRs
cC�sq||_t|_ts*t|�|_nz|j�Wd|jrc|jdk	rc|jj�nX||_dS(s�Sets the exception information of a ``Future.``

        Preserves tracebacks on Python 2.

        .. versionadded:: 4.0
        N(	R#tTrueR$t_GC_CYCLE_FINALIZERSRR%R3RR(RR((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR6s		cC�s|jstd��ndS(Ns1DummyFuture does not support blocking for results(R!t	Exception(R((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR,#s	cC�s_t|_xF|jD];}y||�Wqtk
rMtjd||�qXqWd|_dS(NsException in callback %r for %r(R:R!R&R<RR/R(Rtcb((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR3's	
	cC�sE|js
dStj|j�}tjd|dj|�j��dS(Ns+Future %r exception was never retrieved: %sR(R$RRR#RRRR(Rttb((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR5s
		N(RR
RRR'R(R)R*R+RR.R/R2R4R9RR6R,R3R;R(((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyR~s$#	
						
							
cC�s
t|t�S(N(t
isinstancetFUTURES(tx((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt	is_futureHst
DummyExecutorcB�seZd�Zed�ZRS(cO�sNt�}y|j|||��Wn$tk
rI|jtj��nX|S(N(tTracebackFutureR4R<R6tsysR(RR1targstkwargstfuture((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytsubmitMs	
cC�sdS(N((Rtwait((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytshutdownUs(RR
RIR:RK(((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyRCLs	c�sx�fd�}|r*�r*td��nt|�dkrJ||d�St|�dkrttdt|���n|S(s+Decorator to run a synchronous method asynchronously on an executor.

    The decorated method may be called with a ``callback`` keyword
    argument and returns a future.

    The `.IOLoop` and executor to be used are determined by the ``io_loop``
    and ``executor`` attributes of ``self``. To use different attributes,
    pass keyword arguments to the decorator::

        @run_on_executor(executor='_thread_pool')
        def foo(self):
            pass

    .. versionchanged:: 4.2
       Added keyword arguments to use alternative attributes.
    c�sL�jdd���jdd��tj�����fd��}|S(Ntexecutortio_loopc�sb|jdd��t|��j�|||�}�r^t|��j|�fd��n|S(Ntcallbackc�s�|j��S(N(R.(RH(RN(s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt<lambda>us(tpopRR8RIt
add_future(RRFRGRH(RLR1RM(RNs8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytwrapperos!(tgett	functoolstwraps(R1RR(RG(RLR1RMs8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytrun_on_executor_decoratorls$s*cannot combine positional and keyword argsiisexpected 1 argument, got %d(t
ValueErrortlen(RFRGRV((RGs8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytrun_on_executor[sc�s4t�d��tj����fd��}|S(szDecorator to make a function that returns via callback return a
    `Future`.

    The wrapped function should take a ``callback`` keyword argument
    and invoke it with one argument when it has finished.  To signal failure,
    the function can simply raise an exception (which will be
    captured by the `.StackContext` and passed along to the ``Future``).

    From the caller's perspective, the callback argument is optional.
    If one is given, it will be invoked when the function is complete
    with `Future.result()` as an argument.  If the function fails, the
    callback will not be run and an exception will be raised into the
    surrounding `.StackContext`.

    If no callback is given, the caller should use the ``Future`` to
    wait for the function to complete (perhaps by yielding it in a
    `.gen.engine` function, or passing it to `.IOLoop.add_future`).

    Usage:

    .. testcode::

        @return_future
        def future_func(arg1, arg2, callback):
            # Do stuff (possibly asynchronous)
            callback(result)

        @gen.engine
        def caller(callback):
            yield future_func(arg1, arg2)
            callback()

    ..

    Note that ``@return_future`` and ``@gen.engine`` can be applied to the
    same function, provided ``@return_future`` appears first.  However,
    consider using ``@gen.coroutine`` instead of this combination.
    RNc�s�t���jt�fd�||�\�}}�fd�}d}t|��Ly.�||�}|dk	r�td��nWntj�}�nXWdQX|dk	r��j�n�dk	r��fd�}�j	t
|��n�S(Nc�s
�j|�S(N(R4(tvalue(RH(s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyRO�sc�s�j|||f�tS(N(R6R:(ttypRZR>(RH(s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pythandle_error�ssC@return_future should not be used with functions that return valuesc�s6|j�}|tkr"��n�|j��dS(N(R.t
_NO_RESULT(RHR.(RN(s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytrun_callback�s
(RDtreplaceR]RRRRERR.R2R(RFRGR\RR.R^(tftreplacer(RNRHs8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyRR�s*	


(RRTRU(R`RR((R`Ras8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt
return_future�s'!*c�s#��fd�}�j|�dS(s�Chain two futures together so that when one completes, so does the other.

    The result (success or failure) of ``a`` will be copied to ``b``, unless
    ``b`` has already been completed or cancelled by the time ``a`` finishes.
    c�s��j�rdSt�t�rVt�t�rV�j�dk	rV�j�j��n;�j�dk	r~�j�j��n�j�j	��dS(N(
R*R?RDRRR6R/R9R4R.(RH(tatb(s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytcopy�sN(R2(RcRdRe((RcRds8/usr/lib64/python2.7/site-packages/tornado/concurrent.pytchain_future�s(ii('Rt
__future__RRRRRTtplatformRREttornado.logRttornado.stack_contextRRttornado.utilRRt
concurrentR	tImportErrorRtpython_implementationtversion_infoR;R<RtobjectRRRDR@RBRCtdummy_executorRYR]RbRf(((s8/usr/lib64/python2.7/site-packages/tornado/concurrent.pyt<module>s8"

H�				&		V

Zerion Mini Shell 1.0