K i `dZddlZddlmZddlZddlZddlZddlmZddl Z ddl m Z m Z m Z m Z mZe jdZGddeZej&Zej&efZd e d efd ZGd d ej.ZeZde de d e fdZeZdeddedd dfdZddded dfdZddded dfdZ ddde e e!e ee ejDfd dfdZ#e jHddde dgdfd dfdZ%e jHdd de d gdfd dfd!Z%ddde d"d dfd#Z%y)$aUtilities for working with ``Future`` objects. Tornado previously provided its own ``Future`` class, but now uses `asyncio.Future`. This module contains utility functions for working with `asyncio.Future` in a way that is backwards-compatible with Tornado's old ``Future`` implementation. While this module is an important part of Tornado's internal implementation, applications rarely need to interact with it directly. N)futures)app_log)AnyCallableOptionalTupleUnion_Tc eZdZy)ReturnValueIgnoredErrorN)__name__ __module__ __qualname__X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/concurrent.pyr r *srr xreturnc"t|tSN) isinstanceFUTURES)rs r is_futurer4s a !!rcveZdZdedefdededdfdZejdk\r dd e d e dd fd Z y dd e dd fd Z y ) DummyExecutorfn.argskwargsrfutures.Future[_T]ctj} t|||i||S#t$r"t |t j Y|SwxYwr)rFuture"future_set_result_unless_cancelled Exceptionfuture_set_exc_infosysexc_info)selfrrrfutures rsubmitzDummyExecutor.submit9sV! 8 .vr47J67J K  8   7  8s*'AA) waitcancel_futuresNcyrr)r'r,r-s rshutdownzDummyExecutor.shutdownE rcyrr)r'r,s rr/zDummyExecutor.shutdownJr0r)TF)T) r rrrr rr)r% version_infoboolr/rrrrr8sm37#,/;>  6!  d t     rrrrcdtdtdtfffd }|r r tdt|dk(r ||dSt|dk7rtdt||S) aEDecorator to run a synchronous method asynchronously on an executor. Returns a future. The executor to be used is determined by the ``executor`` attributes of ``self``. To use a different attribute name, pass a keyword argument to the decorator:: @run_on_executor(executor='_thread_pool') def foo(self): pass This decorator should not be confused with the similarly-named `.IOLoop.run_in_executor`. In general, using ``run_in_executor`` when *calling* a blocking method is recommended instead of using this decorator when *defining* a method. If compatibility with older versions of Tornado is required, consider defining an executor and using ``executor.submit()`` at the call site. .. versionchanged:: 4.2 Added keyword arguments to use alternative attributes. .. versionchanged:: 5.0 Always uses the current IOLoop instead of ``self.io_loop``. .. versionchanged:: 5.1 Returns a `.Future` compatible with ``await`` instead of a `concurrent.futures.Future`. .. deprecated:: 5.1 The ``callback`` argument is deprecated and will be removed in 6.0. The decorator itself is discouraged in new code but will not be removed in 6.0. .. versionchanged:: 6.0 The ``callback`` argument was removed. rr.c jddtjdtdtdtdtffd }|S)Nexecutorr'rrrcvt}t|j|g|i|}t|||Sr)r!getattrr) chain_future)r'rr async_future conc_futurer6rs rwrapperzCrun_on_executor..run_on_executor_decorator..wrappers?!8L8'$188TSDSFSK l 3 r)get functoolswrapsrr!)rr<r6rs` @rrun_on_executor_decoratorz2run_on_executor..run_on_executor_decorator|sQ::j*5   # c S V   rz*cannot combine positional and keyword argsrzexpected 1 argument, got %d)rr! ValueErrorlen)rrr@s ` rrun_on_executorrDQspV h 8CK3H  EFF 4yA~(a11 Ta6D BB $$ra) Future[_T]rbcdfd }t|tr t||yddlm}|j j ||y)ajChain 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. .. versionchanged:: 5.0 Now accepts both Tornado/asyncio `Future` objects and `concurrent.futures.Future`. Ncjryt|dr+|jt|jy|j }|j |yj |jy)Nr&)donehasattrr&r$ exception set_exception set_resultresult)rEa_excrGs rcopyzchain_future..copysc 668  1j !ajjl&> 1::< 0KKME & QXXZ(rr)IOLoop)rErFrN)rr!future_add_done_callbacktornado.iolooprRcurrent add_future)rErGrQrRs ` rr9r9s: )!V D) *##At,rr(z%Union[futures.Future[_T], Future[_T]]valuecH|js|j|yy)zSet the given ``value`` as the `Future`'s result, if not cancelled. Avoids ``asyncio.InvalidStateError`` when calling ``set_result()`` on a cancelled `asyncio.Future`. .. versionadded:: 5.0 N) cancelledrN)r(rWs rr"r"s#    %  rexccv|js|j|ytjd|y)aSet the given ``exc`` as the `Future`'s exception. If the Future is already canceled, logs the exception instead. If this logging is not desired, the caller should explicitly check the state of the Future and call ``Future.set_exception`` instead of this wrapper. Avoids ``asyncio.InvalidStateError`` when calling ``set_exception()`` on a cancelled `asyncio.Future`. .. versionadded:: 6.0 z$Exception after Future was cancelled)r&N)rYrMrerror)r(rZs r%future_set_exception_unless_cancelledr]s-    S! rrcallbackcyrrr(r_s rrSrS rrFcyrrras rrSrSrbr).NcX|jr ||y|j|y)aLArrange to call ``callback`` when ``future`` is complete. ``callback`` is invoked with one argument, the ``future``. If ``future`` is already done, ``callback`` is invoked immediately. This may differ from the behavior of ``Future.add_done_callback``, which makes no such guarantee. .. versionadded:: 5.0 N)rJadd_done_callbackras rrSrSs#{{}  *r)&__doc__asyncio concurrentrr>r%types tornado.logrtypingrrrrr TypeVarr r#r r!rr3rExecutorrdummy_executorrDobject _NO_RESULTr9r" BaseExceptionr]type TracebackTyper$overloadrSrrrrus   88V^^D i  >>6 """"G$$,=%3=%#=%(=%@X "- /0"- /0"- "-J ! 3 !<> !  !L 3L:GL L,? 3?/%:M:M1NN?  ?0  ,46J5KT5Q,R      $,l^T-A$B    + 3+?G ?R+ +r