U
    c6{i•  ã                   @  s¼   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dddddœdd„Zdd„ Zdd„ Zdd„ Zdd„ ZddddgZdS )zY
Extended thread dispatching support.

For basic support see reactor threading API docs.
é    )ÚannotationsN)ÚCallableÚTypeVar)Ú	ParamSpec)Údefer)ÚIReactorFromThreads)Úfailure)Ú
ThreadPoolÚ_PÚ_Rr   r	   zCallable[_P, _R]z_P.argsz	_P.kwargszdefer.Deferred[_R])ÚreactorÚ
threadpoolÚfÚargsÚkwargsÚreturnc                   s8   t  ¡ ‰ ddddœ‡ ‡fdd„}|j||f|ž|Ž ˆ 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.
    Úboolz_R | BaseExceptionÚNone)ÚsuccessÚresultr   c                   s&   | rˆ  ˆ j|¡ nˆ  ˆ j|¡ d S ©N)ÚcallFromThreadÚcallbackZerrback)r   r   ©Údr   © ú</tmp/pip-unpacked-wheel-hfbvu0bz/twisted/internet/threads.pyÚonResult9   s    z#deferToThreadPool.<locals>.onResult)r   ZDeferredZcallInThreadWithCallback)r   r   r   r   r   r   r   r   r   ÚdeferToThreadPool   s    r   c                 O  s$   ddl m} t|| ¡ | f|ž|Ž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   ZgetThreadPool)r   r   r   r   r   r   r   ÚdeferToThreadD   s    r!   c                 C  s   | D ]\}}}|||Ž qdS )z"
    Run a list of functions.
    Nr   )Ú	tupleListr   r   r   r   r   r   Ú_runMultipleU   s    r#   c                 C  s   ddl m} | t| ¡ dS )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   ZcallInThreadr#   )r"   r   r   r   r   ÚcallMultipleInThread]   s    r$   c                   sD   t   ¡ ‰‡ ‡‡‡fdd„}|  |¡ ˆ ¡ }t|tjƒr@| ¡  |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                    s"   t jˆfˆ žˆŽ} |  ˆj¡ d S r   )r   ZmaybeDeferredZaddBothÚput)r   ©Úar   ÚkwÚqueuer   r   Ú_callFromThread€   s    z/blockingCallFromThread.<locals>._callFromThread)ÚQueuer   ÚgetÚ
isinstancer   ZFailureZraiseException)r   r   r'   r(   r*   r   r   r&   r   ÚblockingCallFromThreadh   s    
r.   )Ú__doc__Ú
__future__r   r)   r+   Útypingr   r   Ztyping_extensionsr   r    r   Ztwisted.internet.interfacesr   Ztwisted.pythonr   Ztwisted.python.threadpoolr	   r
   r   r   r!   r#   r$   r.   Ú__all__r   r   r   r   Ú<module>   s(   *$ü