ó
[³XMc           @   su   d  Z  d d l Z d d l m Z d d l m Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 d d d g Z d S(   s}   
Extended thread dispatching support.

For basic support see reactor threading API docs.

Maintainer: Itamar Shtull-Trauring
iÿÿÿÿN(   t   failure(   t   deferc            s8   t  j ƒ  ‰ ‡ ‡  f d †  } | j | | | | Ž ˆ S(   s6  
    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            s3   |  r ˆ j  ˆ  j | ƒ n ˆ j  ˆ  j | ƒ d  S(   N(   t   callFromThreadt   callbackt   errback(   t   successt   result(   t   dt   reactor(    s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   onResult+   s    (   R   t   Deferredt   callInThreadWithCallback(   R   t
   threadpoolt   ft   argst   kwargsR	   (    (   R   R   s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   deferToThreadPool   s    c         O   s,   d d l  m } t | | j ƒ  |  | | Ž S(   sw  
    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.
    iÿÿÿÿ(   R   (   t   twisted.internetR   R   t   getThreadPool(   R   R   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   deferToThread6   s    c         C   s+   x$ |  D] \ } } } | | | Ž  q Wd S(   s"   
    Run a list of functions.
    N(    (   t	   tupleListR   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   _runMultipleG   s    c         C   s$   d d l  m } | j t |  ƒ d S(   s   
    Run a list of functions in the same thread.

    tupleList should be a list of (function, argsList, kwargsDict) tuples.
    iÿÿÿÿ(   R   N(   R   R   t   callInThreadR   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   callMultipleInThreadO   s    c            s`   t  j  ƒ  ‰ ‡  ‡ ‡ ‡ f d †  } |  j | ƒ ˆ j ƒ  } t | t j ƒ r\ | j ƒ  n  | S(   s‹  
    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: 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 ˆ ˆ  ˆ Ž }  |  j ˆ j ƒ d  S(   N(   R   t   maybeDeferredt   addBotht   put(   R   (   t   aR   t   queuet   kw(    s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   _callFromThreadp   s    (   t   QueueR   t   gett
   isinstanceR    t   Failuret   raiseException(   R   R   R   R   R   R   (    (   R   R   R   R   s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   blockingCallFromThreadY   s    R   R   R   R$   (   t   __doc__R   t   twisted.pythonR    R   R   R   R   R   R   R$   t   __all__(    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/internet/threads.pyt   <module>
   s   	$			
	!	