Ë
    ojŠ  ã                  óÈ   — d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ  ed
«      Z ed«      Z	 	 	 	 	 	 	 	 	 	 	 	 dd„Zd„ Zd„ Zd„ Zd„ Zg d¢Zy)zY
Extended thread dispatching support.

For basic support see reactor threading API docs.
é    )ÚannotationsN)ÚCallableÚTypeVar)Ú	ParamSpec)Údefer)ÚIReactorFromThreads)Úfailure)Ú
ThreadPoolÚ_PÚ_Rc                ón   ‡ ‡— t        j                  «       Šdˆˆ fd„} |j                  ||g|¢­i |¤Ž ‰S )a3  
    Call the function C{f} using a thread from the given threadpool and return
    the result as a Deferred.

    This function is only used by client code which is maintaining its own
    threadpool.  To run a function in the reactor's threadpool, use
    C{deferToThread}.

    @param reactor: The reactor in whose main thread the Deferred will be
        invoked.

    @param threadpool: An object which supports the C{callInThreadWithCallback}
        method of C{twisted.python.threadpool.ThreadPool}.

    @param f: The function to call.
    @param args: positional arguments to pass to f.
    @param kwargs: keyword arguments to pass to f.

    @return: A Deferred which fires a callback with the result of f, or an
        errback with a L{twisted.python.failure.Failure} if f throws an
        exception.
    c                ó|   •— | r‰j                  ‰j                  |«       y ‰j                  ‰j                  |«       y ©N)ÚcallFromThreadÚcallbackÚerrback)ÚsuccessÚresultÚdÚreactors     €€ú:/usr/lib/python3/dist-packages/twisted/internet/threads.pyÚonResultz#deferToThreadPool.<locals>.onResult9   s.   ø€ ÙØ×"Ñ" 1§:¡:¨vÕ6à×"Ñ" 1§9¡9¨fÕ5ó    )r   Úboolr   z_R | BaseExceptionÚreturnÚNone)r   ÚDeferredÚcallInThreadWithCallback)r   Ú
threadpoolÚfÚargsÚkwargsr   r   s   `     @r   ÚdeferToThreadPoolr#      s9   ù€ ô: "ŸN™NÓ,€Aö6ð (€J×'Ñ'¨°!ÐE°dÒE¸fÒEà€Hr   c                óL   — ddl m} t        ||j                  «       | g|¢­i |¤ŽS )at  
    Run a function in a thread and return the result as a Deferred.

    @param f: The function to call.
    @param args: positional arguments to pass to f.
    @param kwargs: keyword arguments to pass to f.

    @return: A Deferred which fires a callback with the result of f,
    or an errback with a L{twisted.python.failure.Failure} if f throws
    an exception.
    r   ©r   )Útwisted.internetr   r#   ÚgetThreadPool)r    r!   r"   r   s       r   ÚdeferToThreadr(   D   s)   € õ )ä˜W g×&;Ñ&;Ó&=¸qÐRÀ4ÒRÈ6ÑRÐRr   c                ó*   — | D ]  \  }}} ||i |¤Ž Œ y)z"
    Run a list of functions.
    N© )Ú	tupleListr    r!   r"   s       r   Ú_runMultipler,   U   s&   € ð %ò ‰ˆˆ4�Ù	ˆ4Ð�6Óñr   c                ó<   — ddl m} |j                  t        | «       y)z�
    Run a list of functions in the same thread.

    tupleList should be a list of (function, argsList, kwargsDict) tuples.
    r   r%   N)r&   r   ÚcallInThreadr,   )r+   r   s     r   ÚcallMultipleInThreadr/   ]   s   € õ )à×Ñœ yÕ1r   c                óÜ   ‡‡‡‡— t        j                   «       Šˆˆˆˆfd„}| j                  |«       ‰j                  «       }t        |t        j
                  «      r|j                  «        |S )a•  
    Run a function in the reactor from a thread, and wait for the result
    synchronously.  If the function returns a L{Deferred}, wait for its
    result and return that.

    @param reactor: The L{IReactorThreads} provider which will be used to
        schedule the function call.
    @param f: the callable to run in the reactor thread
    @type f: any callable.
    @param a: the arguments to pass to C{f}.
    @param kw: the keyword arguments to pass to C{f}.

    @return: the result of the L{Deferred} returned by C{f}, or the result
        of C{f} if it returns anything other than a L{Deferred}.

    @raise Exception: If C{f} raises a synchronous exception,
        C{blockingCallFromThread} will raise that exception.  If C{f}
        returns a L{Deferred} which fires with a L{Failure},
        C{blockingCallFromThread} will raise that failure's exception (see
        L{Failure.raiseException}).
    c                 ón   •— t        j                  ‰g‰¢­i ‰¤Ž} | j                  ‰j                  «       y r   )r   ÚmaybeDeferredÚaddBothÚput)r   Úar    ÚkwÚqueues    €€€€r   Ú_callFromThreadz/blockingCallFromThread.<locals>._callFromThread€   s-   ø€ Ü×$Ñ$ QÐ1¨Ò1¨bÑ1ˆØ�‰�u—y‘yÕ!r   )ÚQueuer   ÚgetÚ
isinstancer	   ÚFailureÚraiseException)r   r    r5   r6   r8   r   r7   s    ```  @r   ÚblockingCallFromThreadr>   h   sO   û€ ô, �K‰K‹M€E÷"ð ×Ñ˜?Ô+Ø�Y‰Y‹[€FÜ�&œ'Ÿ/™/Ô*Ø×ÑÔØ€Mr   )r(   r#   r/   r>   )r   r   r   r
   r    zCallable[_P, _R]r!   z_P.argsr"   z	_P.kwargsr   zdefer.Deferred[_R])Ú__doc__Ú
__future__r   r7   r9   Útypingr   r   r   r&   r   Útwisted.internet.interfacesr   Útwisted.pythonr	   Útwisted.python.threadpoolr
   r   r   r#   r(   r,   r/   r>   Ú__all__r*   r   r   ú<module>rF      s‘   ðñõ #ã ß $å å "Ý ;Ý "Ý 0áˆtƒ_€ÙˆTƒ]€ð'Ø ð'àð'ð ð'ð ð	'ð
 ð'ð ó'òTSò"ò2ò òF�r   