ó
ßüÚ\c           @   sì   d  Z  d Z d d l Z d d l m Z d d l Z y d d l Z Wn e k
 rc d d l Z n Xd d l	 Z	 d d l
 Z
 d d l Z e
 j ƒ  Z e a d „  Z e j e ƒ d e f d „  ƒ  YZ d „  Z d	 e j f d
 „  ƒ  YZ d S(   s   Implements ThreadPoolExecutor.s"   Brian Quinlan (brian@sweetapp.com)iÿÿÿÿN(   t   _basec          C   sa   t  a t t j ƒ  ƒ }  x! |  D] \ } } | j d  ƒ q Wx |  D] \ } } | j ƒ  qC Wd  S(   N(   t   Truet	   _shutdownt   listt   _threads_queuest   itemst   putt   Nonet   join(   R   t   tt   q(    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt   _python_exit(   s    t	   _WorkItemc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   futuret   fnt   argst   kwargs(   t   selfR   R   R   R   (    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt   __init__6   s    			c         C   si   |  j  j ƒ  s d  Sy |  j |  j |  j Ž  } Wn# t k
 rT } |  j  j | ƒ n X|  j  j | ƒ d  S(   N(   R   t   set_running_or_notify_cancelR   R   R   t   BaseExceptiont   set_exceptiont
   set_result(   R   t   resultt   e(    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt   run<   s    (   t   __name__t
   __module__R   R   (    (    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyR   5   s   	c         C   s¨   yz xs t  rx | j d t  ƒ } | d  k	 r= | j ƒ  ~ q n  |  ƒ  } t sa | d  k sa | j rr | j d  ƒ d  S~ q WWn' t k
 r£ t j j	 d d t  ƒn Xd  S(   Nt   blocks   Exception in workert   exc_info(
   R   t   getR   R   R   R   R   R    t   LOGGERt   critical(   t   executor_referencet
   work_queuet	   work_itemt   executor(    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt   _workerH   s    	
	t   ThreadPoolExecutorc           B   se   e  Z e j ƒ  Z d d  d „ Z d „  Z e j	 j j
 e _
 d „  Z e d „ Z e j	 j j
 e _
 RS(   t    c         C   sœ   | d k r% t j ƒ  p d d } n  | d k r@ t d ƒ ‚ n  | |  _ t j ƒ  |  _ t ƒ  |  _	 t
 |  _ t j ƒ  |  _ | p’ d t |  j ƒ |  _ d S(   s  Initializes a new ThreadPoolExecutor instance.

        Args:
            max_workers: The maximum number of threads that can be used to
                execute the given calls.
            thread_name_prefix: An optional name prefix to give our threads.
        i   i   i    s"   max_workers must be greater than 0s   ThreadPoolExecutor-%dN(   R   t   ost	   cpu_countt
   ValueErrort   _max_workerst   queuet   Queuet   _work_queuet   sett   _threadst   FalseR   t	   threadingt   Lockt   _shutdown_lockt   nextt   _countert   _thread_name_prefix(   R   t   max_workerst   thread_name_prefix(    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyR   d   s    		c      	   O   sk   |  j  \ |  j r" t d ƒ ‚ n  t j ƒ  } t | | | | ƒ } |  j j | ƒ |  j ƒ  | SWd  QXd  S(   Ns*   cannot schedule new futures after shutdown(	   R4   R   t   RuntimeErrorR    t   FutureR   R.   R   t   _adjust_thread_count(   R   R   R   R   t   ft   w(    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt   submit|   s    
	
c      	   C   s°   |  j  d „ } t |  j ƒ } | |  j k  r¬ d |  j p< |  | f } t j d | d t d t j	 |  | ƒ |  j  f ƒ } t
 | _ | j ƒ  |  j j | ƒ |  j  t | <n  d  S(   Nc         S   s   | j  d  ƒ d  S(   N(   R   R   (   t   _R
   (    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt
   weakref_cb   s    s   %s_%dt   namet   targetR   (   R.   t   lenR0   R+   R7   R2   t   ThreadR%   t   weakreft   refR   t   daemont   startt   addR   (   R   RA   t   num_threadst   thread_nameR	   (    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyR<   Š   s    		
c         C   sT   |  j   t |  _ |  j j d  ƒ Wd  QX| rP x |  j D] } | j ƒ  q9 Wn  d  S(   N(   R4   R   R   R.   R   R   R0   R   (   R   t   waitR	   (    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt   shutdownŸ   s    
	N(   R   R   t	   itertoolst   countR6   R   R   R?   R    t   Executort   __doc__R<   R   RN   (    (    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyR&   _   s   		(   RR   t
   __author__t   atexitt   concurrent.futuresR    RO   R,   t   ImportErrorR-   R2   RF   R(   t   WeakKeyDictionaryR   R1   R   R   t   registert   objectR   R%   RQ   R&   (    (    (    sE   lib/python2.7/site-packages/distributed/_concurrent_futures_thread.pyt   <module>   s$   	
	