ó
šßÈ[c           @   sæ   d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z y d d l Z Wn e k
 ru d d l	 Z n Xd Z
 e ƒ  Z e a d „  Z d „  Z e j e ƒ d e f d	 „  ƒ  YZ d
 „  Z d e j f d „  ƒ  YZ d S(   s   Implements ThreadPoolExecutor.iÿÿÿÿNi   (   t   _bases"   Brian Quinlan (brian@sweetapp.com)c          C   s=   t  a x0 t D]( }  |  ƒ  } | d  k	 r | j ƒ  q q Wd  S(   N(   t   Truet	   _shutdownt   _thread_referencest   Nonet   join(   t   thread_referencet   thread(    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   _python_exit'   s
    	c          C   s:   x3 t  t ƒ D]% }  |  ƒ  d k r t j |  ƒ q q Wd S(   s  Remove inactive threads from _thread_references.

    Should be called periodically to prevent memory leaks in scenarios such as:
        >>> while True:
        ...    t = ThreadPoolExecutor(max_workers=5)
        ...    t.map(int, ['1', '2', '3', '4', '5'])
    N(   t   setR   R   t   discard(   R   (    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   _remove_dead_thread_references0   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   (    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   __init__A   s    			c         C   sw   |  j  j ƒ  s d  Sy |  j |  j |  j Ž  } Wn1 t k
 rb t j ƒ  d } |  j  j | ƒ n X|  j  j	 | ƒ d  S(   Ni   (
   R   t   set_running_or_notify_cancelR   R   R   t   BaseExceptiont   syst   exc_infot   set_exceptiont
   set_result(   R   t   resultt   e(    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   runG   s    (   t   __name__t
   __module__R   R   (    (    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyR   @   s   	c         C   s§   yy xr t  rw y | j d t  d d ƒ } Wn? t j k
 ri |  ƒ  } t s_ | d  k s_ | j rc d  S~ q X| j ƒ  q WWn' t k
 r¢ t j	 j
 d d t  ƒn Xd  S(   Nt   blockt   timeoutgš™™™™™¹?s   Exception in workerR   (   R   t   gett   queuet   EmptyR   R   R   R   R    t   LOGGERt   critical(   t   executor_referencet
   work_queuet	   work_itemt   executor(    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   _workerT   s    		t   ThreadPoolExecutorc           B   sS   e  Z d  „  Z d „  Z e j j j e _ d „  Z e d „ Z	 e j j	 j e	 _ RS(   c         C   sG   t  ƒ  | |  _ t j ƒ  |  _ t ƒ  |  _ t |  _ t	 j
 ƒ  |  _ d S(   sÕ   Initializes a new ThreadPoolExecutor instance.

        Parameters
        ----------
        max_workers
            The maximum number of threads that can be used to
            execute the given calls.
        N(   R   t   _max_workersR!   t   Queuet   _work_queueR	   t   _threadst   FalseR   t	   threadingt   Lockt   _shutdown_lock(   R   t   max_workers(    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyR   i   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(	   R2   R   t   RuntimeErrorR    t   FutureR   R-   t   putt   _adjust_thread_count(   R   R   R   R   t   ft   w(    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   submitz   s    
	
c         C   s‚   t  |  j ƒ |  j k  r~ t j d t d t j |  ƒ |  j f ƒ } t	 | _
 | j ƒ  |  j j | ƒ t j t j | ƒ ƒ n  d  S(   Nt   targetR   (   t   lenR.   R+   R0   t   ThreadR)   t   weakreft   refR-   R   t   daemont   startt   addR   (   R   t   t(    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyR7   ˆ   s    	
c         C   sD   |  j   t |  _ Wd  QX| r@ x |  j D] } | j ƒ  q) Wn  d  S(   N(   R2   R   R   R.   R   (   R   t   waitRC   (    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   shutdown“   s
    
(
   R   R   R   R:   R    t   Executort   __doc__R7   R   RE   (    (    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyR*   h   s   			(   RG   t   atexitR0   R>   R   t    R    R!   t   ImportErrorR,   t
   __author__R	   R   R/   R   R   R   t   registert   objectR   R)   RF   R*   (    (    (    sB   lib/python2.7/site-packages/astropy/utils/compat/futures/thread.pyt   <module>   s$   					