ó
¤Ž©Qc           @`  s´   d  Z  d d l m Z m Z y d d l m Z Wn! e k
 rS d d l m Z n Xd d l Z d d l Z d d l	 Z	 d d l
 m Z m Z m Z e ƒ  Z d d d „  ƒ  YZ d S(	   sº   
twisted.python.threadpool: a pool of threads to which we dispatch tasks.

In most cases you can just use C{reactor.callInThread} and friends
instead of creating a thread pool directly.
i    (   t   divisiont   absolute_import(   t   QueueN(   t   logt   contextt   failuret
   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 e j 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.

    L{callInThread} and L{stop} should only be called from
    a single thread, unless you make a subclass where L{stop} and
    L{_startSomeWorkers} are synchronized.
    i   i   i    c         C`  sy   | d k s t  d ƒ ‚ | | k s0 t  d ƒ ‚ t 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   AssertionErrorR   t   qt   mint   maxt   namet   waiterst   threadst   working(   t   selft
   minthreadst
   maxthreadsR   (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   __init__/   s    					c         C`  s    t  |  _ t |  _ |  j ƒ  d S(   s'   
        Start the threadpool.
        N(   t   Falset   joinedt   Truet   startedt   adjustPoolsize(   R   (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   startA   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(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   startAWorkerK   s
    "c         C`  s#   |  j  j t ƒ |  j d 8_ d  S(   Ni   (   R   t   putt
   WorkerStopR   (   R   (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   stopAWorkerS   s    c         C`  s&   | |  _  t j |  |  j |  j ƒ d  S(   N(   t   __dict__R   R   R	   R
   (   R   t   state(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   __setstate__X   s    	c         C`  s$   i  } |  j  | d <|  j | d <| S(   NR	   R
   (   R	   R
   (   R   R%   (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   __getstate__]   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(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   _startSomeWorkersd   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 C{func}

        @param **kw: keyword args to be passed to C{func}
        N(   t   callInThreadWithCallbackt   None(   R   t   funct   argst   kw(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   callInThreadk   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 C{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 C{onResult} function
        must not block and should perform as little work as possible.

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

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

            Optionally, C{onResult} may be C{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 C{func}

        @param **kwargs: keyword arguments to be passed to C{func}
        Niÿÿÿÿ(	   R   R   t   theContextTrackert   currentContextt   contextsR   R!   R   R+   (   R   t   onResultR.   R/   R0   t   ctxt   o(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyR,   x   s    		c         c`  s+   | j  | ƒ z	 d VWd | j | ƒ Xd S(   s+  
        Manages adding and removing this worker from a list of workers
        in a particular state.

        @param stateList: the list managing workers in this state

        @param workerThread: the thread the worker is running in, used to
            represent the worker in stateList
        N(   R   t   remove(   R   t	   stateListt   workerThread(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   _workerStateŸ   s    	c   
   	   C`  s_  |  j  ƒ  } |  j j ƒ  } x-| t k	 rJ|  j |  j | ƒ Œ | \ } } } } } ~ y" t j | | | | Ž } t }	 WnA t	 }	 | d k r® t j | t j ƒ d } q¾ t j ƒ  } n X~ ~ ~ Wd QX| d k	 ry t j | | |	 | ƒ Wqt j | t j ƒ qXn  ~ ~ ~ |  j |  j | ƒ  |  j j ƒ  } Wd QXq W|  j j | ƒ d S(   s½   
        Method used as target of the created threads: retrieve a 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   R-   R   t   errR   t   FailureR   R   R8   (
   R   t   ctR7   R6   t   functionR/   t   kwargsR5   t   resultt   success(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyR   ±   s2    
		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(    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   stopÙ   s    	c         C`  sÓ   | d  k r |  j } n  | d  k r0 |  j } n  | d k sH t d ƒ ‚ | | k s` t d ƒ ‚ | |  _ | |  _ |  j s 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(	   R-   R	   R
   R   R   R   R#   R    R+   (   R   R   R   (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/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   (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt	   dumpStats  s    N(    t   __name__t
   __module__t   __doc__R	   R
   R   R   R   R   R-   R   t	   threadingt   ThreadR   t   staticmethodR<   R   R   R    R#   R&   R'   R+   R1   R,   t
   contextlibt   contextmanagerR;   R   RI   R   RL   (    (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyR      s.   		
							'	(	(    (   RO   t
   __future__R    R   R   t   ImportErrorRK   RS   RP   RF   t   twisted.pythonR   R   R   t   objectR"   R   (    (    (    sK   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/python/threadpool.pyt   <module>
   s   	