ó
[³XMc           @   s±   d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z m Z m	 Z	 d d l
 m Z d d l m Z e ƒ  Z d d
 d „  ƒ  YZ d d d	 „  ƒ  YZ d S(   s°   
twisted.threadpool: a pool of threads to which we dispatch tasks.

In most cases you can just use reactor.callInThread and friends
instead of creating a thread pool directly.
iÿÿÿÿN(   t   logt   contextt   failure(   t   deprecatedModuleAttribute(   t   Versiont
   ThreadPoolc           B   sé   e  Z d  Z d Z d Z e Z e Z d Z d Z
 e j Z e e j ƒ Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d „  Z RS(   s  
    This class (hopefully) generalizes the functionality of a pool of
    threads to which work can be dispatched.

    callInThread() and stop() should only be called from
    a single thread, unless you make a subclass where stop() and
    _startSomeWorkers() are synchronized.
    i   i   i    c         C   sv   | d k s t  d ‚ | | k s* t  d ‚ t j d ƒ |  _ | |  _ | |  _ | |  _ g  |  _ g  |  _ g  |  _ d S(   s®   
        Create a new threadpool.

        @param minthreads: minimum number of threads in the pool

        @param maxthreads: maximum number of threads in the pool
        i    s   minimum is negatives   minimum is greater than maximumN(	   t   AssertionErrort   Queuet   qt   mint   maxt   namet   waiterst   threadst   working(   t   selft
   minthreadst
   maxthreadsR   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   __init__0   s    					c         C   s    t  |  _ t |  _ |  j ƒ  d S(   s'   
        Start the threadpool.
        N(   t   Falset   joinedt   Truet   startedt   adjustPoolsize(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   startB   s    		c         C   sj   |  j  d 7_  d |  j p$ t |  ƒ |  j  f } |  j d |  j d | ƒ } |  j j | ƒ | j ƒ  d  S(   Ni   s   PoolThread-%s-%st   targetR   (   t   workersR   t   idt   threadFactoryt   _workerR   t   appendR   (   R   R   t	   newThread(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   startAWorkerK   s
    "c         C   s#   |  j  j t ƒ |  j d 8_ d  S(   Ni   (   R   t   putt
   WorkerStopR   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   stopAWorkerR   s    c         C   s&   | |  _  t j |  |  j |  j ƒ d  S(   N(   t   __dict__R   R   R	   R
   (   R   t   state(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   __setstate__V   s    	c         C   s$   i  } |  j  | d <|  j | d <| S(   NR	   R
   (   R	   R
   (   R   R%   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   __getstate__Z   s    c         C   sL   |  j  j ƒ  t |  j ƒ } x) |  j t |  j | ƒ k  rG |  j ƒ  q Wd  S(   N(   R   t   qsizet   lenR   R   R	   R
   R    (   R   t
   neededSize(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   _startSomeWorkers`   s    c         O   s-   t  j d t d d ƒ|  j | | | Ž d S(   sp   
        DEPRECATED: use L{callInThread} instead.

        Dispatch a function to be a run in a thread.
        sF   dispatch() is deprecated since Twisted 8.0, use callInThread() insteadt
   stackleveli   N(   t   warningst   warnt   DeprecationWarningt   callInThread(   R   t   ownert   funct   argst   kw(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   dispatchg   s    	c         O   s   |  j  d | | | Ž d S(   sý   
        Call a callable object in a separate thread.

        @param func: callable object to be called in separate thread

        @param *args: positional arguments to be passed to func

        @param **kw: keyword args to be passed to func
        N(   t   callInThreadWithCallbackt   None(   R   R2   R3   R4   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR0   s   s    
c         O   sb   |  j  r d St j j ƒ  j d } | | | | | f } |  j j | ƒ |  j r^ |  j ƒ  n  d S(   sæ  
        Call a callable object in a separate thread and call onResult
        with the return value, or a L{twisted.python.failure.Failure}
        if the callable raises an exception.

        The callable is allowed to block, but the onResult function
        must not block and should perform as little work as possible.

        A typical action for onResult for a threadpool used with a
        Twisted reactor would be to schedule a Deferred to fire in the
        main reactor thread using C{.callFromThread}.  Note that
        onResult is called inside the separate thread, not inside the
        reactor thread.

        @param onResult: a callable with the signature (success, result).
            If the callable returns normally, onResult is called with
            (True, result) where result is the return value of the callable.
            If the callable throws an exception, onResult is called with
            (False, failure).

            Optionally, onResult may be None, in which case it is not
            called at all.

        @param func: callable object to be called in separate thread

        @param *args: positional arguments to be passed to func

        @param **kwargs: keyword arguments to be passed to func
        Niÿÿÿÿ(	   R   R   t   theContextTrackert   currentContextt   contextsR   R!   R   R+   (   R   t   onResultR2   R3   R4   t   ctxt   o(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR6   €   s    		c         C   sB   y t  | | | ƒ } Wn | t j ƒ  d ƒ n X| | ƒ d  S(   Ni   (   t   applyt   syst   exc_info(   R   t   callbackt   errbackR2   R3   t   kwargst   result(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   _runWithCallback§   s
    c         O   s9   t  j d t d d ƒ|  j |  j | | | | | ƒ d S(   s  
        DEPRECATED: use L{twisted.internet.threads.deferToThread} instead.

        Dispatch a function, returning the result to a callback function.

        The callback function will be called in the thread - make sure it is
        thread-safe.
        sm   dispatchWithCallback() is deprecated since Twisted 8.0, use twisted.internet.threads.deferToThread() instead.R,   i   N(   R-   R.   R/   R0   RE   (   R   R1   RA   RB   R2   R3   R4   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   dispatchWithCallback°   s    		c   
      C   sg  |  j  ƒ  } |  j j ƒ  } x5| t k	 rR|  j j | ƒ | \ } } } } } ~ y" t j | | | | Ž } t }	 WnA t	 }	 | d k r¨ t j | t j ƒ d } q¸ t j ƒ  } n X~ ~ ~ |  j j | ƒ | d k	 ry t j | | |	 | ƒ Wqt j | t j ƒ qXn  ~ ~ ~ |  j j | ƒ |  j j ƒ  } |  j j | ƒ q W|  j j | ƒ d S(   s»   
        Method used as target of the created threads: retrieve task to run
        from the threadpool, run it, and proceed to the next task until
        threadpool is stopped.
        N(   t   currentThreadR   t   getR"   R   R   R   t   callR   R   R7   R    t   errR   t   Failuret   removeR   R   (
   R   t   ctR=   R<   t   functionR3   RC   R;   RD   t   success(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR   Á   s6    
			c         C   si   t  |  _ t j |  j ƒ } x, |  j rI |  j j t ƒ |  j d 8_ q Wx | D] } | j ƒ  qQ Wd S(   s9   
        Shutdown the threads in the threadpool.
        i   N(	   R   R   t   copyR   R   R   R!   R"   t   join(   R   R   t   thread(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   stopë   s    	c         C   sÍ   | d  k r |  j } n  | d  k r0 |  j } n  | d k sE t d ‚ | | k sZ t d ‚ | |  _ | |  _ |  j sy d  Sx  |  j |  j k r› |  j ƒ  q| Wx  |  j |  j k  r¾ |  j ƒ  qŸ W|  j ƒ  d  S(   Ni    s   minimum is negatives   minimum is greater than maximum(	   R7   R	   R
   R   R   R   R#   R    R+   (   R   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR   ú   s    			c         C   sW   t  j d |  j j ƒ t  j d |  j ƒ t  j d |  j ƒ t  j d |  j ƒ d  S(   Ns	   queue: %ss   waiters: %ss   workers: %ss	   total: %s(   R    t   msgR   t   queueR   R   R   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt	   dumpStats  s    N(    t   __name__t
   __module__t   __doc__R	   R
   R   R   R   R   R7   R   t	   threadingt   ThreadR   t   staticmethodRG   R   R   R    R#   R&   R'   R+   R5   R0   R6   RE   RF   R   RS   R   RV   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR      s2   											'				*	t   ThreadSafeListc           B   sT   e  Z d  Z e e d d d d ƒ d e  d ƒ d „  Z d „  Z d	 „  Z d
 „  Z RS(   sÏ   
    In Jython 2.1 lists aren't thread-safe, so this wraps it.  Newer versions
    of Jython are completely different than 2.1, so this class is deprecated
    to make way for future versions of Jython.
    t   Twistedi
   i   i    s\   This was an internal implementation detail of support for Jython 2.1, which is now obsolete.R]   c         C   s   t  j ƒ  |  _ g  |  _ d  S(   N(   RZ   t   Lockt   lockt   l(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR   &  s    c         C   s6   |  j  j ƒ  z |  j j | ƒ Wd  |  j  j ƒ  Xd  S(   N(   R`   t   acquireRa   R   t   release(   R   t   i(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR   *  s    c         C   s6   |  j  j ƒ  z |  j j | ƒ Wd  |  j  j ƒ  Xd  S(   N(   R`   Rb   Ra   RL   Rc   (   R   Rd   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyRL   1  s    c         C   s   t  |  j ƒ S(   N(   R)   Ra   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   __len__8  s    (	   RW   RX   RY   R   R   R   R   RL   Re   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR]     s   
			(    (    (   RY   R   RZ   RP   R?   R-   t   twisted.pythonR    R   R   t   twisted.python.deprecateR   t   twisted.python.versionsR   t   objectR"   R   R]   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt   <module>   s   	ü