
O'^c           @@  sj  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l 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 d  d l m Z d d l m Z m Z m Z d d l m Z d d l m Z m Z m Z d d	 l m Z m Z d d
 l m Z d d l m Z d d l m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& d d l' m( Z( m) Z) m* Z* m+ Z+ m, Z, m- Z- d d l m. Z. m/ Z/ e j0 d  d k Z1 e j2   d k rd d l3 m4 Z5 e j6 Z7 n d  d l m8 Z5 e j7 Z7 y e j9 Z9 Wn e: k
 rd Z9 n Xe j0 d d f k r4e j; Z< n	 e j< Z< e= d   e> e  D  Z? d  Z@ d ZA d ZB d  ZC d ZD d ZE d ZF d ZG d  ZH d ZI d ZJ eK e d eL  ZM d ZN eK e d d   ZH e jO   ZP e jQ ZQ d   ZR d   ZS d   ZT d   ZU d   ZV eL d   ZW d! e< f d"     YZX d# eY f d$     YZZ d% eY f d&     YZ[ d'   Z\ d( e f d)     YZ] d* e f d+     YZ^ d, e^ f d-     YZ_ d. e^ f d/     YZ` d0 e^ f d1     YZa d2 e^ f d3     YZb d4 ec f d5     YZd d6 ec f d7     YZe d8 ee f d9     YZf d: ec f d;     YZg d< eg f d=     YZh d> ed f d?     YZi d S(@   i    (   t   absolute_importN(   t   deque(   t   partiali   (   t   Eventt   Processt	   cpu_count(   t   util(   t   pickle_loadst   reset_signalst   restart_state(   t	   get_errnot   send_offset(   t   ExceptionInfo(   t   DummyProcess(   t   CoroStopt   RestartFreqExceededt   SoftTimeLimitExceededt
   Terminatedt   TimeLimitExceededt   TimeoutErrort   WorkerLostError(   t   Emptyt   Queuet   ranget   valuest   reraiset	   monotonic(   t   Finalizet   debugi   t   Windows(   t   kill_processtree(   t   killg    _Bc         c@  s3   |  ]) } | j  d   r t t |  | f Vq d S(   t   SIGN(   t
   startswitht   getattrt   signal(   t   .0t   n(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>I   s    i   i   i   t   SIGUSR1g      $@t   EX_OKc         C@  sJ   y |  j  } Wn t k
 r& d  } n X| d  k rF t t  |  j    S| S(   N(   R   t   AttributeErrort   NoneR   t   fileno(   t
   connectiont   native(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _get_send_offsetu   s    
c         C@  s[   |  p	 d d k  rN y d j  |  t |   SWqN t k
 rJ d j  |   SXn  d j  |   S(   Ni    s   signal {0} ({1})s
   signal {0}s   exitcode {0}(   t   formatt   SIGMAPt   KeyError(   t   status(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   human_status   s    c         C@  s   t  t |     S(   N(   t   listt   map(   t   args(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   mapstar   s    c         C@  s   t  t j |  d |  d   S(   Ni    i   (   R3   t	   itertoolst   starmap(   R5   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   starmapstar   s    c         O@  s&   t  j r" t  j j |  | |  n  d  S(   N(   R   t   _loggert   error(   t   msgR5   t   kwargs(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR;      s    	c         C@  s&   |  t  j   k	 r" |  j |  n  d  S(   N(   t	   threadingt   currentThreadt   stop(   t   threadt   timeout(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   stop_if_not_current   s    t   LaxBoundedSemaphorec           B@  se   e  Z d  Z d d
 d  Z d   Z e rH d   Z d   Z d   Z	 n d   Z	 d   Z d	   Z RS(   s^   Semaphore that checks that # release is <= # acquires,
    but ignores if # releases >= value.i   c         C@  s9   t  r t j |  |  n t j |  | |  | |  _ d  S(   N(   t   PY3t
   _Semaphoret   __init__t   _initial_value(   t   selft   valuet   verbose(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG      s    c         C@  s   |  j  d 8_  |  j   d  S(   Ni   (   RH   t   acquire(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   shrink   s    c         C@  sH   |  j  } | 3 |  j |  j k  r> |  j d 7_ | j   n  Wd  QXd  S(   Ni   (   t   _condt   _valueRH   t
   notify_all(   RI   t   cond(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   release   s
    	c         C@  s*   x# |  j  |  j k  r% t j |   q Wd  S(   N(   RO   RH   RF   RR   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   clear   s    c         C@  s?   |  j  0 |  j d 7_ |  j d 7_ |  j  j   Wd  QXd  S(   Ni   (   RN   RH   RO   t   notify(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   grow   s    
c         C@  sB   |  j  } | - |  j d 7_ |  j d 7_ | j   Wd  QXd  S(   Ni   (   t   _Semaphore__condRH   t   _Semaphore__valueRT   (   RI   RQ   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRU      s
    	c         C@  sH   |  j  } | 3 |  j |  j k  r> |  j d 7_ | j   n  Wd  QXd  S(   Ni   (   RV   RW   RH   t	   notifyAll(   RI   RQ   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRR      s
    	c         C@  s*   x# |  j  |  j k  r% t j |   q Wd  S(   N(   RW   RH   RF   RR   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRS      s    N(
   t   __name__t
   __module__t   __doc__R)   RG   RM   RE   RR   RS   RU   (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRD      s   					t   MaybeEncodingErrorc           B@  s)   e  Z d  Z d   Z d   Z d   Z RS(   sV   Wraps possible unpickleable errors, so they can be
    safely sent through the socket.c         C@  sA   t  |  |  _ t  |  |  _ t t |   j |  j |  j  d  S(   N(   t   reprt   excRJ   t   superR\   RG   (   RI   R^   RJ   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG      s    c         C@  s   d t  |   S(   Ns   <MaybeEncodingError: %s>(   t   str(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   __repr__   s    c         C@  s   d |  j  |  j f S(   Ns)   Error sending result: '%r'. Reason: '%r'.(   RJ   R^   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   __str__   s    (   RY   RZ   R[   RG   Ra   Rb   (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR\      s   		t   WorkersJoinedc           B@  s   e  Z d  Z RS(   s   All workers have terminated.(   RY   RZ   R[   (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRc      s   c         C@  s   t     d  S(   N(   R   (   t   signumt   frame(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   soft_timeout_sighandler   s    t   Workerc           B@  s   e  Z e Z e Z d d d d d d e d   Z d   Z d   Z	 d   Z
 d d  Z d   Z d   Z d   Z e e d d  Z d	   Z d
   Z e d  Z d   Z RS(   c
   
      C@  s   | d  k s0 t |  t k r* | d k s0 t  | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _	 | | | |  _
 |  _ |  _ |  j   t t |   j   d  S(   Ni    (   R)   t   typet   intt   AssertionErrort   initializert   initargst   maxtaskst	   _shutdownt   on_exitt   sigprotectiont   inqt   outqt   synqt   _make_shortcutsR_   Rg   RG   (
   RI   Rq   Rr   Rs   Rk   Rl   Rm   t   sentinelRo   Rp   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG      s    0						
c         C@  s7   |  j  |  j |  j |  j |  j |  j |  j |  j f f S(   N(   t	   __class__Rq   Rr   Rs   Rk   Rl   Rm   Rn   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt
   __reduce__  s    c         C@  s   |  j  j j   |  _ |  j j j   |  _ |  j ru |  j j j   |  _ |  j j j   |  _	 t
 |  j j  |  _ n d  |  _ |  _	 |  _ |  j  j j |  _ |  j j j |  _ t
 |  j  j  |  _ d  S(   N(   Rq   t   _writerR*   t   inqW_fdRr   t   _readert   outqR_fdRs   t   synqR_fdt   synqW_fdR-   t   send_syn_offsetR)   t   _send_syn_offsett   sendt
   _quick_putt   recvt
   _quick_gett   send_job_offset(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRt     s    	c         @  s   t  j   d  g  d     f d  } | t  _ t j   } |  j   |  j   |  j d |  zd y t  j |  j d |   Wn@ t	 k
 r } t
 d |  | d d |  j |  d |  n XWd  |  j |  d d   Xd  S(   Nc         @  s   |   d <    S(   Ni    (    (   R1   (   t   _exitt	   _exitcode(    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   exit  s    
t   pids   Pool process %r error: %rt   exc_infoi   i    (   t   sysR   R)   t   ost   getpidt   _make_child_methodst
   after_forkt   on_loop_startt   workloopt	   ExceptionR;   t   _do_exit(   RI   R   R   R^   (    (   R   R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   run  s    			

 c         C@  s   | d  k r! | r t n t } n  |  j d  k	 rC |  j | |  n  t j d k r z- |  j j t | | f f  t	 j
 d  Wd  t j |  Xn t j |  d  S(   Nt   win32i   (   R)   t
   EX_FAILURER'   Ro   R   t   platformRr   t   putt   DEATHt   timet   sleepR   R   (   RI   R   t   exitcodeR^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   .  s    c         C@  s   d  S(   N(    (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   >  s    c         C@  s   t  |  _ |  j   d  S(   N(   t   Truet   _controlled_terminationt	   terminate(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   terminate_controlledA  s    	c         C@  s   | S(   N(    (   RI   t   result(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   prepare_resultE  s    c         @  s0  | p t  j   }  j j }  j }  j }  j }  j }  j }	  j	      f d   }
 d } x| d  k s | r| | k  r|	   } | ro | \ } } | t k s t  | \ } } } } } | t | | |   | | f f    r|
 |  } | sqo qn  y t | | | |    f } Wn  t k
 rSt t   f } n Xy  | t | | | | f f  Wn} t k
 r} t j   \ } } } zN t | | d  } t t | | f  } | t | | t | f | f f  Wd  ~ Xn X| d 7} qo qo W| d |  | r,| | k r(t St St S(   Nc         @  s   d } x | d k r: t  d |   j j j   d d n      } | r{ | \ } } | t k re t S| t k sw t  t S| d 7} q	 Wd  S(   Ni    i<   s(   !!!WAIT FOR ACK TIMEOUT: job:%r fd:%r!!!R   i   (	   R;   Rs   Rz   R*   t   NACKt   Falset   ACKRj   R   (   t   jidt   it   reqt   type_R5   (   t   _wait_for_synRI   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   wait_for_synS  s    	i    i   s   worker exiting after %d tasks(   R   R   Rr   R   Ry   R}   Rm   R   t   wait_for_jobR   R)   t   TASKRj   R   R   R   R   R   t   READYR   R   R\   t
   EX_RECYCLER   R'   (   RI   R   t   nowR   R   Ry   R}   Rm   R   R   R   t	   completedR   R   t   args_t   jobR   t   funR5   R=   t   confirmR   R^   t   _t   tbt   wrappedt   einfo(    (   R   RI   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   H  sP    						!	"	 &c         C@  s   t  |  j d  r% |  j j j   n  t  |  j d  rJ |  j j j   n  |  j d  k	 rl |  j |  j   n  t	 d |  j
  t d  k	 r t j t t  n  y t j t j t j  Wn t k
 r n Xd  S(   NRx   Rz   t   full(   t   hasattrRq   Rx   t   closeRr   Rz   Rk   R)   Rl   R   Rp   t   SIG_SOFT_TIMEOUTR#   Rf   t   SIGINTt   SIG_IGNR(   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         @  s   | j   t | d  ro | j j   t | d  rZ | j rZ | j  t  f d  } q~    f d   } n  f d   } | S(   NRz   t   get_payloadc         @  s   t  |      f S(   N(   R   (   RB   t   loads(   R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _recv  s    c         @  s#     |   r t     f St d  f S(   N(   R   R   R)   (   RB   (   t   _pollt   get(    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         @  s9   y t    d |   f SWn t j k
 r4 t d  f SXd  S(   NRB   (   R   R   R   R   R)   (   RB   (   R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    (   R   R   Rz   t   pollR   R   (   RI   t   connR   (    (   R   R   R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _make_recv_method  s    		c         C@  s=   |  j  |  j  |  _ |  j r0 |  j  |  j  n d  |  _ d  S(   N(   t   _make_protected_receiveRq   R   Rs   R)   R   (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         @  sC   |  j  |    |  j r$ |  j j n d   t    f d  } | S(   Nc         @  s    r(    r( |  d  t  t   n  y    d  \ } } | sG d  SWnT t t f k
 r } t |  t j k ry d  S|  d t |  j	  t  t
   n X| d  k r |  d  t  t
   n  | S(   Ns   worker got sentinel -- exitingg      ?s   worker got %s -- exiting(   t
   SystemExitR'   R)   t   EOFErrort   IOErrorR
   t   errnot   EINTRRh   RY   R   (   R   t   readyR   R^   (   t   _receivet   should_shutdown(    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   receive  s     

(   R   Rn   t   is_setR)   R   (   RI   R   R   (    (   R   R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    N(    (   RY   RZ   R   R   t   _job_terminatedR)   R   RG   Rw   Rt   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRg      s"   								<		t
   PoolThreadc           B@  sJ   e  Z d    Z d   Z d   Z d   Z d d  Z d   Z d   Z	 RS(   c         O@  s,   t  j |   t |  _ t |  _ t |  _ d  S(   N(   R   RG   t   RUNt   _stateR   t   _was_startedR   t   daemon(   RI   R5   R=   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG     s    		c         C@  s   y |  j    SWn t k
 rb } t d t |   j | d d t t j   t j	  t
 j   n? t k
 r } t d t |   j | d d t j d  n Xd  S(   Ns   Thread %r crashed: %rR   i   (   t   bodyR   R;   Rh   RY   t   _killR   R   R#   t   SIGTERMR   R   R   R   (   RI   R^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         O@  s&   t  |  _ t t |   j | |   d  S(   N(   R   R   R_   R   t   start(   RI   R5   R=   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    	c         C@  s   d  S(   N(    (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_stop_not_started  s    c         C@  s(   |  j  r |  j |  d  S|  j   d  S(   N(   R   t   joinR   (   RI   RB   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR@     s    	c         C@  s   t  |  _ d  S(   N(   t	   TERMINATER   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         C@  s   t  |  _ d  S(   N(   t   CLOSER   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    N(
   RY   RZ   RG   R   R   R   R)   R@   R   R   (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s   					t
   Supervisorc           B@  s   e  Z d    Z d   Z RS(   c         C@  s    | |  _  t t |   j   d  S(   N(   t   poolR_   R   RG   (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG     s    	c         C@  s  t  d  t j d  |  j } y | j } t d | j d  | _ xL t d  D]> } |  j t k rR | j t k rR | j	   t j d  qR qR W| | _ x9 |  j t k r | j t k r | j	   t j d  q WWn( t
 k
 r| j   | j     n Xt  d  d  S(   Ns   worker handler startingg?i
   i   g?s   worker handler exiting(   R   R   R   R   R	   t
   _processesR   R   R   t   _maintain_poolR   R   R   (   RI   R   t
   prev_stateR   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s&    
		
	!


(   RY   RZ   RG   R   (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s   	t   TaskHandlerc           B@  s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C@  s;   | |  _  | |  _ | |  _ | |  _ t t |   j   d  S(   N(   t	   taskqueueR   t   outqueueR   R_   R   RG   (   RI   R   R   R   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG   $  s
    				c         C@  s  |  j  } |  j } x t | j d   D] \ } } y d } x t |  D]P \ } } |  j rj t d  Pn  y | |  WqG t k
 r t d  PqG XqG W| r t d  | | d  n  w% PWq% t	 k
 r } t
 d | d d Pq% Xq% Wt d  |  j   d  S(	   Nis'   task handler found thread._state != RUNs   could not put task on queues   doing set_length()i   s   Task Handler ERROR: %rR   s   task handler got sentinel(   R   R   t   iterR   R)   t	   enumerateR   R   R   R   R;   t   tell_others(   RI   R   R   t   taskseqt
   set_lengthR   t   taskR^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   +  s0    			

	
	
c         C@  s   |  j  } |  j } |  j } y@ t d  | j d   t d  x | D] } | d   qF WWn t k
 rx t d  n Xt d  d  S(   Ns/   task handler sending sentinel to result handlers(   task handler sending sentinel to workerss/   task handler got IOError when sending sentinelss   task handler exiting(   R   R   R   R   R)   R   (   RI   R   R   R   t   p(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   I  s    			

c         C@  s   |  j    d  S(   N(   R   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   \  s    (   RY   RZ   RG   R   R   R   (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   "  s   			t   TimeoutHandlerc           B@  sP   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   c         C@  sD   | |  _  | |  _ | |  _ | |  _ d  |  _ t t |   j   d  S(   N(	   t	   processest   cachet   t_softt   t_hardR)   t   _itR_   R   RG   (   RI   R   R   R   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG   b  s    					c         @  s&   t    f d   t |  j  D d  S(   Nc         3@  s0   |  ]& \ } } | j    k r | | f Vq d  S(   N(   R   (   R$   R   t   proc(   R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>l  s    	(   NN(   t   nextR   R   R)   (   RI   R   (    (   R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _process_by_pidj  s    c         C@  s   t  d |  |  j | j  \ } } | s/ d  S| j d t  y t | j t  Wn. t k
 r } t |  t	 j
 k r   q n Xd  S(   Ns   soft time limit exceeded for %rt   soft(   R   R   t   _worker_pidt   handle_timeoutR   R   R   t   OSErrorR
   R   t   ESRCH(   RI   R   t   processt   _indexR^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_soft_timeoutp  s    c         C@  s   | j    r d  St d |  y t | j   Wn- t k
 r_ | j | j t t   f  n X|  j | j	  \ } } | j
 d t  | r |  j |  n  d  S(   Ns   hard time limit exceeded for %rR   (   R   R   R   t   _timeoutt   _sett   _jobR   R   R   R   R   t   _trywaitkill(   RI   R   R   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_hard_timeout  s     c         C@  s   t  d | j  y | j   Wn t k
 r1 n X| j j d d  rK d  St  d | j  y t | j t  Wn t k
 r n Xd  S(   Ns   timeout: sending TERM to %sRB   g?s/   timeout: TERM timed-out, now sending KILL to %s(	   R   t   _nameR   R   t   _popent   waitR   R   t   SIGKILL(   RI   t   worker(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         #@  s=  |  j    |  j |  j } } t   } |  j } |  j } d   } x |  j t k r8| rw t   f d   | D  } n  x t   j	    D] \ } } | j
 }	 | j }
 |
 d  k r | }
 n  | j } | d  k r | } n  | |	 |  r | |  q | | k r | |	 |
  r | |  | j |  q q Wd  VqC Wd  S(   Nc         S@  s-   |  s | r t  St   |  | k r) t Sd  S(   N(   R   R   R   (   R   RB   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt
   _timed_out  s    c         3@  s!   |  ] } |   k r | Vq d  S(   N(    (   R$   t   k(   R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>  s    (   R   R   R   t   setR   R  R   R   R3   t   itemst   _time_acceptedt   _soft_timeoutR)   R   t   add(   RI   R   R   t   dirtyR   R  R
  R   R   t   ack_timet   soft_timeoutt   hard_timeout(    (   R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   handle_timeouts  s.    										
c         C@  sa   xP |  j  t k rR y( x! |  j   D] } t j d  q" WWq t k
 rN Pq Xq Wt d  d  S(   Ng      ?s   timeout handler exiting(   R   R   R  R   R   R   R   (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    	c         G@  sS   |  j  d  k r! |  j   |  _  n  y t |  j   Wn t k
 rN d  |  _  n Xd  S(   N(   R   R)   R  R   t   StopIteration(   RI   R5   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   handle_event  s    (
   RY   RZ   RG   R   R   R  R  R  R   R  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   `  s   						#		t   ResultHandlerc           B@  sS   e  Z d    Z d   Z d   Z d d  Z d d d  Z d   Z e	 d  Z
 RS(	   c
   
      C@  s   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  |  _ t	 |  _
 | |  _ |	 |  _ |  j   t t |   j   d  S(   N(   R   R   R   R   t   join_exited_workerst   putlockR	   R)   R   R   t   _shutdown_completet   check_timeoutst   on_job_readyt   _make_methodsR_   R  RG   (
   RI   R   R   R   R   R  R  R	   R  R  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG     s    											
c         C@  s   |  j  d t  d  S(   NR  (   t   finish_at_shutdownR   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         @  s   |  j    |  j  |  j  |  j     f d   }     f d   } d   } i | t 6| t 6| t 6 |  _  f d   } | |  _ d  S(   Nc         @  sE   d  _  y   |  j | | | |  Wn t t f k
 r@ n Xd  S(   Ni    (   t   Rt   _ackR0   R(   (   R   R   t   time_acceptedR   R}   (   R   R	   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_ack  s
    	c         @  s    d  k	 r"  |  | | |  n  y   |  } Wn t k
 rD d  SX| j   sm  d  k	 rm  j   qm n  y | j | |  Wn t k
 r n Xd  S(   N(   R)   R0   R   RR   R  (   R   R   t   objRy   t   item(   R   R  R  (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_ready  s    c         S@  sL   y t  j |  t j  Wn. t k
 rG } t |  t j k rH   qH n Xd  S(   N(   R   R   R#   R   R   R
   R   R   (   R   R   R^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_death  s
    c         @  sF   |  \ } } y   | |   Wn! t  k
 rA t d | |  n Xd  S(   Ns   Unknown job state: %s (args=%s)(   R0   R   (   R   t   stateR5   (   t   state_handlers(    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_state_change  s
    (	   R   R  R	   R  R   R   R   R)  R*  (   RI   R#  R&  R'  R*  (    (   R   R  R  R	   R)  s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    					g      ?c         c@  s   |  j  } |  j } x y | |  \ } } Wn/ t t f k
 r\ } t d |  t    n X|  j r |  j t k s{ t  t d  t    n  | r | d  k r t d  t    n  | |  | d k r Pq n Pd  Vq Wd  S(   Ns    result handler got %r -- exitings,   result handler found thread._state=TERMINATEs   result handler got sentineli    (
   R   R*  R   R   R   R   R   R   Rj   R)   (   RI   RB   R   R*  R   R   R^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _process_result  s*    			


c         C@  sn   |  j  t k rj |  j d  k r3 |  j d  |  _ n  y t |  j  Wqj t t f k
 rf d  |  _ qj Xn  d  S(   Ni    (   R   R   R   R)   R+  R   R  R   (   RI   R*   t   events(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  :  s    c         C@  si   t  d  zM xF |  j t k rU y x |  j d  D] } q2 WWq t k
 rQ Pq Xq WWd  |  j   Xd  S(   Ns   result handler startingg      ?(   R   R   R   R+  R   R  (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   C  s    
c         C@  s  t  |  _ |  j } |  j } |  j } |  j } |  j } |  j } |  j } d  }	 x| rm|  j
 t k rm| d  k	 r| |   n  y | d  \ }
 } Wn' t t f k
 r } t d |  d  SX|
 r | d  k r t d  qQ n  | |  n  y | d t   WqQ t k
 rit   } |	 s$| }	 qj| |	 d k rBt d  Pn  t d t t | |	 d d    qQ XqQ Wt | d	  rt d
  y5 x. t d  D]  } | j j   sPn  |   qWWqt t f k
 rqXn  t d t |  |  j
  d  S(   Ng      ?s    result handler got %r -- exitings&   result handler ignoring extra sentinelt   shutdowng      @s!   result handler exiting: timed outs6   result handler: all workers terminated, timeout in %ssi    Rz   s"   ensuring that outqueue is not fulli
   s7   result handler exiting: len(cache)=%s, thread._state=%s(   R   R  R   R   R   R   R  R  R*  R)   R   R   R   R   R   Rc   R   t   abst   minR   R   Rz   t   len(   RI   R  R   R   R   R   R  R  R*  t   time_terminateR   R   R^   R   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  O  sX    								

		
&
N(   RY   RZ   RG   R   R  R+  R)   R  R   R   R  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s   			0		t   Poolc           B@  s  e  Z d  Z e Z e Z e Z e Z e Z e Z d8 d8 d9 d8 d8 d8 d8 d8 d d8 d8 d8 d8 e
 d8 e e e d8 d  Z d   Z d   Z d   Z d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z e d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d d  Z! d d  Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d    Z* d!   Z+ d: i  d"  Z, d8 d#  Z- d8 d8 d8 d$  Z. d8 d%  Z/ d d8 d&  Z0 d d8 d'  Z1 d; i  d8 d8 d8 d8 d8 d8 d8 d8 d< d8 d(  Z2 d)   Z3 d8 d*  Z4 d8 d8 d8 d+  Z5 d8 d8 d8 d,  Z6 e7 d-    Z8 d.   Z9 d/   Z: d0   Z; e7 d1    Z< d2   Z= d3   Z> e7 d4    Z? e@ d5    ZA e@ d6    ZB eC d7    ZD RS(=   sS   
    Class which supports an async version of applying functions to arguments.
    i   c         K@  sg  | |  _  |  j   t   |  _ i  |  _ t |  _ | |  _ | |  _ | |  _	 | |  _
 | |  _ | |  _ | pp t |  _ |
 |  _ | |  _ | |  _ | |  _ | |  _ i  |  _ | |  _ | r t d  k r t j t d   d  } n  | d  k r |  j   n | |  _ | pt |  j d  |  _ t | |	 p/d  |  _ | d  k	 r`t  |  r`t! d   n  | d  k	 rt  |  rt! d   n  g  |  _" i  |  _# | |  _$ | pt% |  j  |  _& x$ t' |  j  D] } |  j( |  qW|  j) |   |  _* | r
|  j* j+   n  |  j, |  j |  j- |  j. |  j"  |  _/ | rG|  j/ j+   n  |  j0 |  j" |  j |  j |  j  |  _1 t2   |  _3 t4 |  _5 |  j d  k	 s|  j d  k	 r|  j6   n  d  |  _7 | s|  j1 j8 |  _7 n  |  j9   |  _: |  j: j8 |  _; | r|  j: j+   n  t< |  |  j= d |  j |  j> |  j. |  j" |  j* |  j/ |  j: |  j |  j1 |  j?   f
 d d |  _@ d  S(	   NsW   Soft timeouts are not supported: on this platform: It does not have the SIGUSR1 signal.id   i   s   initializer must be a callables    on_process_exit must be callableR5   t   exitpriorityi   (A   t   synackt   _setup_queuesR   t
   _taskqueuet   _cacheR   R   RB   R  t   _maxtasksperchildt   _initializert	   _initargst   _on_process_exitt   LOST_WORKER_TIMEOUTt   lost_worker_timeoutt   on_process_upt   on_process_downt   on_timeout_sett   on_timeout_cancelt   threadst   readerst   allow_restartR   R)   t   warningst   warnt   UserWarningR   R   t   roundt   max_restartsR	   t   callablet	   TypeErrort   _poolt	   _poolctrlt   putlocksRD   t   _putlockR   t   _create_worker_processR   t   _worker_handlerR   R   R   t	   _outqueuet   _task_handlerR   t   _timeout_handlert   Lockt   _timeout_handler_mutexR   t   _timeout_handler_startedt   _start_timeout_handlerR  R  t   create_result_handlert   _result_handlert   handle_result_eventR   t   _terminate_poolt   _inqueuet   _help_stuff_finish_argst
   _terminate(   RI   R   Rk   Rl   t   maxtasksperchildRB   R  R=  RI  t   max_restart_freqR>  R?  R@  RA  RB  t	   semaphoreRN  RD  R4  t   on_process_exitR=   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG     s    	
																
	!					c         K@  sC   |  j  |  j |  j |  j |  j |  j |  j |  j |  j |  j	 | 	 S(   N(
   R  RR  R   R7  t   _poll_resultt   _join_exited_workersRO  R	   R  R  (   RI   t   extra_kwargs(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRY    s
    c         C@  s   d  S(   N(    (   RI   R   R   R$  Ry   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s   |  j  |  j |  j f S(   N(   R]  RS  RL  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR^    s    c         C@  s$   y t    SWn t k
 r d SXd  S(   Ni   (   R   t   NotImplementedError(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         G@  s   |  j  j |   S(   N(   RZ  R  (   RI   R5   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR[    s    c         C@  s   d  S(   N(    (   RI   R	  t   queues(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _process_register_queues  s    c         @  s&   t    f d   t |  j  D d  S(   Nc         3@  s0   |  ]& \ } } | j    k r | | f Vq d  S(   N(   R   (   R$   R   R   (   R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>  s    	(   NN(   R   R   RL  R)   (   RI   R   (    (   R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         C@  s   |  j  |  j d  f S(   N(   R]  RR  R)   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   get_process_queues  s    c         C@  s   |  j  r t   n d  } |  j   \ } } } |  j | | | |  j |  j |  j | |  j d |  j	 } |  j
 j |  |  j | | | | f  | j j d d  | _ t | _ | | _ | j   | |  j | j <|  j r |  j |  n  | S(   NRp   R   t
   PoolWorker(   RD  R   R)   Rj  Rg   R9  R:  R8  R;  RB  RL  t   appendRi  t   namet   replaceR   R   t   indexR   RM  R   R>  (   RI   R   Ru   Rq   Rr   Rs   t   w(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRP    s     		
	c         C@  s   d  S(   N(    (   RI   R	  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   process_flush_queues2  s    c         @  s  d	 } x g  t |  j j    D]" } | j   r | j r | ^ q D]J } | pW t   } | j \ } } | | | j k rE |  j | |  qE qE W| r t	 |  j
  r t    n  i  i   } xt t t	 |  j
    D] } |  j
 | } | j }	 | j }
 |
 d	 k s|	 d	 k	 r t d |  |
 d	 k	 rA| j   n  t d |  |  | j <|	 | | j <|	 t t f k rt | d t  rt d | j | j t |	  d d n  |  j |  |  j
 | =|  j | j =q q W rg  |  j
 D] } | j ^ q  x0t |  j j    D]} t    f d   | j   D d	  } | r|  j | |  | j   s4| j |  p}d }	  j |  } | rt | d t  r| j  |	  q|  j! | | |	  q4q| j" } | j# } | r| j$   r|  j | | j  q| r| j$   r|  j | | j  qqWxC t   D]5 } |  j% rE| sj|  j& |  n  |  j% |  qEqEWt | j    Sg  S(
   s   Cleanup after any worker processes which have exited due to
        reaching their specified lifetime. Returns True if any workers were
        cleaned up.
        s!   Supervisor: cleaning up worker %ds   Supervisor: worked %d joinedR   s    Process %r pid:%r exited with %rR   i    c         3@  s-   |  ]# } |  k s! |   k r | Vq d  S(   N(    (   R$   R   (   t   all_pidst   cleaned(    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>d  s    R   N('   R)   R3   R7  R   R   t   _worker_lostR   t   _lost_worker_timeoutt   mark_as_worker_lostR0  RL  Rc   t   reversedR   R   R  R   R   R   R'   R   R"   R   R;   Rm  R2   Rq  RM  R   t   worker_pidst   on_job_process_downR   t   _set_terminatedt   on_job_process_lostt	   _write_tot   _scheduled_fort	   _is_aliveR?  t   _process_cleanup_queues(   RI   R-  R   R   t	   lost_timet   lost_rett	   exitcodesR   R	  R   t   popenRp  t   acked_by_goneR   t   write_tot	   sched_for(    (   Rr  Rs  s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRe  5  st    &"		

				c         C@  s   d  S(   N(    (   RI   R   R	  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_partial_read  s    c         C@  s   d  S(   N(    (   RI   R	  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s   d  S(   N(    (   RI   R   t   pid_gone(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRy    s    c         C@  s   t    | f | _ d  S(   N(   R   Rt  (   RI   R   R   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR{    s    c         C@  sP   y t  d j t |     Wn* t  k
 rK | j d  t t   f  n Xd  S(   Ns   Worker exited prematurely: {0}.(   R   R.   R2   R  R)   R   R   (   RI   R   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRv    s    c         C@  s   |  S(   N(    (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt	   __enter__  s    c         G@  s
   |  j    S(   N(   R   (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   __exit__  s    c         C@  s   d  S(   N(    (   RI   R%   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   on_grow  s    c         C@  s   d  S(   N(    (   RI   R%   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt	   on_shrink  s    c         C@  s   x t  |  j    D]_ \ } } |  j d 8_ |  j rG |  j j   n  | j   |  j d  | | d k r Pq q Wt d   d  S(   Ni   s&   Can't shrink pool. All processes busy!(   R   t   _iterinactiveR   RO  RM   R   R  t
   ValueError(   RI   R%   R   R	  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRM     s    	
c         C@  sP   x< t  |  D]. } |  j d 7_ |  j r |  j j   q q W|  j |  d  S(   Ni   (   R   R   RO  RU   R  (   RI   R%   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRU     s
    	c         c@  s8   x( |  j  D] } |  j |  s
 | Vq
 q
 Wt    d  S(   N(   RL  t   _worker_activeR  (   RI   R	  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s7   x0 t  |  j  D] } | j | j   k r t Sq Wt S(   N(   R   R7  R   Rx  R   R   (   RI   R	  R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s   x t  |  j t |  j   D] } |  j t k r6 d Sy0 | re | | t t f k re |  j j	   n  Wn t
 k
 r |  j j	   n X|  j |  j    t d  q Wd S(   s   Bring the number of pool processes up to the specified number,
        for use after reaping workers which have exited.
        Ns   added worker(   R   R   R0  RL  R   R   R'   R   R	   t   stept
   IndexErrorRP  t   _avail_indexR   (   RI   R  R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _repopulate_pool  s    #c         @  sZ   t  |  j  |  j k  s t  t d   |  j D    t   f d   t |  j  D  S(   Nc         s@  s   |  ] } | j  Vq d  S(   N(   Ro  (   R$   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>  s    c         3@  s!   |  ] } |   k r | Vq d  S(   N(    (   R$   R   (   t   indices(    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>  s    (   R0  RL  R   Rj   R  R   R   (   RI   (    (   R  s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s   |  j    S(   N(   Re  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   did_start_ok  s    c         C@  sY   |  j    } |  j |  x9 t t |   D]% } |  j d k	 r, |  j j   q, q, Wd S(   sF   "Clean up any exited workers and start replacements for them.
        N(   Re  R  R   R0  RO  R)   RR   (   RI   t   joinedR   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s
    c         C@  s   |  j  j t k r |  j t k r y |  j   Wq t k
 rY |  j   |  j     q t k
 r } t |  t	 j
 k r t t t t |   t j   d  n    q Xn  d  S(   Ni   (   RQ  R   R   R   R   R   R   R   R
   R   t   ENOMEMR   t   MemoryErrorR`   R   R   (   RI   R^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   maintain_pool  s    !

c         @  sh   d d l  m } |     _ |     _   j j j   _   j j j   _	   f d   } |   _
 d  S(   Ni    (   t   SimpleQueuec         @  s/     j  j j |   r% t   j   f St d  f S(   N(   RR  Rz   R   R   R   R   R)   (   RB   (   RI   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRd    s    (   t   billiard.queuesR  R]  RR  Rx   R   R   Rz   R   R   Rd  (   RI   R  Rd  (    (   RI   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR5    s    c         C@  sB   |  j  r> |  j ' |  j s5 t |  _ |  j j   n  Wd  QXn  d  S(   N(   RB  RV  RW  R   RT  R   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRX    s
    	
		c         C@  s,   |  j  t k r( |  j | | |  j   Sd S(   s8   
        Equivalent of `func(*args, **kwargs)`.
        N(   R   R   t   apply_asyncR   (   RI   t   funcR5   t   kwds(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   apply  s    c         C@  s/   |  j  t k r+ |  j | | t |  j   Sd S(   s   
        Like `map()` method but the elements of the `iterable` are expected to
        be iterables as well and will be unpacked as arguments. Hence
        `func` and (a, b) becomes func(a, b).
        N(   R   R   t
   _map_asyncR9   R   (   RI   R  t   iterablet	   chunksize(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR8     s    c         C@  s/   |  j  t k r+ |  j | | t | | |  Sd S(   s=   
        Asynchronous version of `starmap()` method.
        N(   R   R   R  R9   (   RI   R  R  R  t   callbackt   error_callback(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   starmap_async  s    c         C@  s,   |  j  t k r( |  j | | |  j   Sd S(   sx   
        Apply `func` to each element in `iterable`, collecting the results
        in a list that is returned.
        N(   R   R   t	   map_asyncR   (   RI   R  R  R  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR4   (  s    c         @  s   |  j  t k r d S| p |  j } | d k ry t |  j d |  |  j j    f d   t |  D  j f   S| d k s t	  t
 j   | |  } t |  j d |  |  j j  f d   t |  D  j f  d    D Sd S(   sP   
        Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
        Ni   R=  c         3@  s6   |  ], \ } } t   j |   | f i  f f Vq d  S(   N(   R   R  (   R$   R   t   x(   R  R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>;  s   c         3@  s6   |  ], \ } } t    j | t | f i  f f Vq d  S(   N(   R   R  R6   (   R$   R   R  (   R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>F  s   c         s@  s"   |  ] } | D] } | Vq q d  S(   N(    (   R$   t   chunkR%  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>J  s    (   R   R   R=  t   IMapIteratorR7  R6  R   R   t   _set_lengthRj   R2  t
   _get_tasks(   RI   R  R  R  R=  t   task_batches(    (   R  R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   imap0  s(    				c         @  s   |  j  t k r d S| p |  j } | d k ry t |  j d |  |  j j    f d   t |  D  j f   S| d k s t	  t
 j   | |  } t |  j d |  |  j j  f d   t |  D  j f  d    D Sd S(   sL   
        Like `imap()` method but ordering of results is arbitrary.
        Ni   R=  c         3@  s6   |  ], \ } } t   j |   | f i  f f Vq d  S(   N(   R   R  (   R$   R   R  (   R  R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>Y  s   c         3@  s6   |  ], \ } } t    j | t | f i  f f Vq d  S(   N(   R   R  R6   (   R$   R   R  (   R   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>e  s   c         s@  s"   |  ] } | D] } | Vq q d  S(   N(    (   R$   R  R%  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>i  s    (   R   R   R=  t   IMapUnorderedIteratorR7  R6  R   R   R  Rj   R2  R  (   RI   R  R  R  R=  R  (    (   R  R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   imap_unorderedL  s(    		c         C@  s  |  j  t k r d S|	 p |  j }	 |
 p. |  j }
 | p= |  j } |	 rn t d k rn t j t	 d   d }	 n  |  j  t k r| d k r |  j
 n | } | r |  j d k	 r |  j j   n  t |  j | | | | |	 |
 | d |  j d |  j d | d |  j r|  j n d d | } |
 s#|	 r0|  j   n  |  j rm|  j j t | j d | | | f f g d f  n% |  j t | j d | | | f f  | Sd S(   s  
        Asynchronous equivalent of `apply()` method.

        Callback is called when the functions return value is ready.
        The accept callback is called when the job is accepted to be executed.

        Simplified the flow is like this:

            >>> def apply_async(func, args, kwds, callback, accept_callback):
            ...     if accept_callback:
            ...         accept_callback()
            ...     retval = func(*args, **kwds)
            ...     if callback:
            ...         callback(retval)

        NsW   Soft timeouts are not supported: on this platform: It does not have the SIGUSR1 signal.R@  RA  t   callbacks_propagatet   send_ackt   correlation_id(   R   R   R  RB   R=  R   R)   RE  RF  RG  RN  RO  RL   t   ApplyResultR7  R@  RA  R4  R  RX  RB  R6  R   R   R  R   (   RI   R  R5   R  R  R  t   accept_callbackt   timeout_callbackt   waitforslotR  RB   R=  R  R  R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  k  s8    	
					%c         C@  s   d  S(   N(    (   RI   t   responseR   R   t   fd(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s   |  j  |  \ } } | d  k	 r y t | | p6 t j  Wn. t k
 rk } t |  t j k r~   q~ q Xt	 | _
 t	 | _ n  d  S(   N(   R   R)   R   R#   R   R   R
   R   R   R   R   R   (   RI   R   t   sigR   R   R^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   terminate_job  s    
	c         C@  s   |  j  | | t | | |  S(   s<   
        Asynchronous equivalent of `map()` method.
        (   R  R6   (   RI   R  R  R  R  R  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c   	      @  s  |  j  t k r d St | d  s1 t |  } n  | d k r{ t t |  t |  j  d  \ } } | r{ | d 7} q{ n  t |  d k r d } n  t j	 | | |  } t
 |  j | t |  | d |  |  j j    f d   t |  D d f   S(   sY   
        Helper function to implement map, starmap and their async counterparts.
        Nt   __len__i   i   i    R  c         3@  s6   |  ], \ } } t   j |   | f i  f f Vq d  S(   N(   R   R  (   R$   R   R  (   t   mapperR   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pys	   <genexpr>  s   (   R   R   R   R3   R)   t   divmodR0  RL  R2  R  t	   MapResultR7  R6  R   R   (	   RI   R  R  R  R  R  R  t   extraR  (    (   R  R   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s     (		c         c@  sD   t  |  } x1 t t j | |   } | s1 d  S|  | f Vq Wd  S(   N(   R   t   tupleR7   t   islice(   R  t   itt   sizeR  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s   t  d   d  S(   Ns:   pool objects cannot be passed between processes or pickled(   Rg  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRw     s    c         C@  sl   t  d  |  j t k rh t |  _ |  j r; |  j j   n  |  j j   |  j j	 d   t |  j  n  d  S(   Ns   closing pool(   R   R   R   R   RO  RS   RQ  R   R6  R   R)   RC   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    
		c         C@  s.   t  d  t |  _ |  j j   |  j   d  S(   Ns   terminating pool(   R   R   R   RQ  R   R_  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    
	c         C@  s   t  |   d  S(   N(   RC   (   t   task_handler(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _stop_task_handler  s    c         C@  s   |  j  t t f k s t  t d  t |  j  t d  |  j |  j  t d  t |  j	  t d  xY t
 |  j  D]H \ } } t d | d t |  j  |  | j d  k	 r} | j   q} q} Wt d  d  S(   Ns   joining worker handlers   joining task handlers   joining result handlers   result handler joineds   joining worker %s/%s (%r)i   s   pool join complete(   R   R   R   Rj   R   RC   RQ  R  RS  RZ  R   RL  R0  R  R)   R   (   RI   R   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    



 c         C@  s(   x! t  |  j  D] } | j   q Wd  S(   N(   R   RM  R  (   RI   t   e(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   restart  s    c         C@  sW   t  d  |  j j   x9 | j   rR |  j j   rR |  j j   t j d  q Wd  S(   Ns7   removing tasks from inqueue until task handler finishedi    (	   R   t   _rlockRL   t   is_aliveRz   R   R   R   R   (   t   inqueueR  RL  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _help_stuff_finish  s
    
c         C@  s   | j  d   d  S(   N(   R   R)   (   t   clsR   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _set_result_sentinel  s    c         C@  s  t  d  | j   | j   | j d   t  d  |  j |
   | j   |  j | |  |	 d  k	 ru |	 j   n  | r t | d d  r t  d  x* | D] } | j   r | j   q q Wn  t  d  |  j |  t  d  | j	   |	 d  k	 rt  d  |	 j	 t
  n  | rt | d d  rt  d	  xI | D]A } | j   r@t  d
 | j  | j d  k	 r| j   qq@q@Wt  d  n  d  S(   Ns   finalizing pools&   helping task handler/workers to finishi    R   s   terminating workerss   joining task handlers   joining result handlers   joining timeout handlers   joining pool workerss   cleaning up worker %ds   pool workers joined(   R   R   R   R)   R  R  R   R~  R  R@   t   TIMEOUT_MAXR  R   R  R   (   R  R   R  R   R   t   worker_handlerR  t   result_handlerR   t   timeout_handlert   help_stuff_finish_argsR   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR\    s<    










c         C@  s    g  |  j  D] } | j j ^ q
 S(   N(   RL  R  Ru   (   RI   Rp  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   process_sentinelsG  s    N(    (    (    (    (E   RY   RZ   R[   Rg   R   R   R   R  R   R)   R   R   RG   RY  R  R^  R   R[  Ri  R   Rj  RP  Rq  Re  R  R  Ry  R{  Rv  R  R  R  R  RM   RU   R  R  R  R  R  R   R  R5  RX  R  R8   R  R4   R  R  R  R  R  R  R  t   staticmethodR  Rw   R   R   R  R   R  R  t   classmethodR  R\  t   propertyR  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR2    s   		Z										R																				
		3				
				2R  c           B@  s   e  Z d Z d Z d Z d d d d d e d d d d d d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d d  Z d	   Z d d
  Z d d  Z d   Z e d  Z d   Z d   Z RS(   c         C@  s   | |  _  t   |  _ t j   |  _ t t  |  _ | |  _	 | |  _
 | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ |
 |  _ | p d |  _ | |  _ t |  _ t |  _ d  |  _ d  |  _ d  |  _ |  | |  j <d  S(   N(    (   R  RU  t   _mutexR>   R   t   _eventR   t   job_counterR  R7  t	   _callbackt   _accept_callbackt   _error_callbackt   _timeout_callbackR   R  Ru  t   _on_timeout_sett   _on_timeout_cancelt   _callbacks_propagatet	   _send_ackR   t	   _acceptedt
   _cancelledR)   R   R  t   _terminated(   RI   R   R  R  R  R  R  RB   R=  R@  RA  R  R  R  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG   U  s,    																	c         C@  s(   d j  d |  j d |  j d |  j    S(   Ns&   <Result: {id} ack:{ack} ready:{ready}>t   idt   ackR   (   R.   R  R  R   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRa   s  s    	c         C@  s   |  j  j   S(   N(   R  t   isSet(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   x  s    c         C@  s   |  j  S(   N(   R  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   accepted{  s    c         C@  s   |  j    s t  |  j S(   N(   R   Rj   t   _success(   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt
   successful~  s    c         C@  s   t  |  _ d S(   s   Only works if synack is used.N(   R   R  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   _cancel  s    c         C@  s   |  j  j |  j d   d  S(   N(   R7  t   popR  R)   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   discard  s    c         C@  s   | |  _  d  S(   N(   R  (   RI   Rd   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         C@  sH   y t  | p d   Wn* t  k
 rC |  j d  t t   f  n Xd  S(   Ni    (   R   R  R)   R   R   (   RI   Rd   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRz    s    c         C@  s   |  j  r |  j  g Sg  S(   N(   R   (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRx    s    c         C@  s   |  j  j |  d  S(   N(   R  R  (   RI   RB   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  sB   |  j  |  |  j   s" t  n  |  j r2 |  j S|  j j  d  S(   N(   R  R   R   R  RO   t	   exception(   RI   RB   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    		c         O@  s]   | rY y | | |   WqY |  j  k
 r0   qY t k
 rU } t d | d d qY Xn  d  S(   Ns"   Pool callback raised exception: %rR   i   (   R  R   R;   (   RI   R   R5   R=   R^   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   safe_apply_callback  s    c         C@  sD   |  j  d  k	 r@ |  j |  j  d | d | r3 |  j n |  j n  d  S(   NR   RB   (   R  R)   R  R  R   (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR     s    c         C@  s   |  j   |  j r# |  j |   n  | \ |  _ |  _ |  j j   |  j rd |  j j |  j	 d   n  |  j r |  j r |  j |  j |  j  n  |  j d  k	 r |  j r |  j r |  j |  j |  j  n  Wd  QXd  S(   N(   R  R  R  RO   R  R  R  R7  R  R  R)   R  R  R  (   RI   R   R$  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    
		c         C@  sn  |  j  _|  j rH |  j rH t |  _ | rD |  j t | |  j |  Sd  St |  _ | |  _ | |  _ |  j	   r |  j
 j |  j d   n  |  j r |  j |  |  j |  j  n  t } |  j r9zN y |  j | |  Wn3 |  j k
 r t }   n t k
 rt } n XWd  |  j r5| r5|  j | | |  j |  SXn  |  j rd| rd|  j | | |  j |  n  Wd  QXd  S(   N(   R  R  R  R   R  R   R  R  R   R   R7  R  R)   R  R  R   R   R  t   _propagate_errorsR   (   RI   R   R"  R   R}   R  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR!    s8    
						 N(    (   RY   RZ   R)   Rt  R|  R}  R<  RG   Ra   R   R  R  R  R  R   Rz  Rx  R  R   R  R   R   R  R!  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  P  s0   											
	R  c           B@  s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C@  s   t  j |  | | d | t |  _ | |  _ d  g | |  _ t g | |  _ d  g | |  _	 d  g | |  _
 | |  _ | d k r d |  _ |  j j   | |  j =n | | t | |  |  _ d  S(   NR  i    (   R  RG   R   R  t   _lengthR)   RO   R   R  R   R  t
   _chunksizet   _number_leftR  R  R  t   bool(   RI   R   R  t   lengthR  R  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG     s    				c         C@  s  | \ } } | r | |  j  | |  j | d |  j +|  j d 8_ |  j d k r |  j rn |  j |  j   n  |  j r |  j j |  j d   n  |  j	 j
   q n] t |  _ | |  _  |  j r |  j |  j   n  |  j r |  j j |  j d   n  |  j	 j
   d  S(   Ni   i    (   RO   R  R  R  R  R7  R  R  R)   R  R  R   R  R  (   RI   R   t   success_resultt   successR   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s"    "						c         G@  s   | |  j  } t | d |  j  |  j  } x> t | |  D]- } t |  j | <| |  j | <| |  j | <q: W|  j   r |  j	 j
 |  j d   n  d  S(   Ni   (   R  R/  R  R   R   R  R   R  R   R7  R  R  R)   (   RI   R   R"  R   R5   R   R@   t   j(    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR!    s    c         C@  s   t  |  j  S(   N(   t   allR  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    c         C@  s    g  |  j  D] } | r
 | ^ q
 S(   N(   R   (   RI   R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRx  "  s    (   RY   RZ   RG   R  R!  R  Rx  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s
   			
	R  c           B@  sb   e  Z d Z e d   Z d   Z d d  Z e Z d   Z	 d   Z
 d   Z d   Z d   Z RS(	   c         C@  s   t  j t  j    |  _ t t  |  _ | |  _ t   |  _	 d |  _
 d  |  _ t |  _ i  |  _ g  |  _ | |  _ |  | |  j <d  S(   Ni    (   R>   t	   ConditionRU  RN   R   R  R  R7  R   t   _itemsR   R)   R  R   t   _readyt	   _unsortedt   _worker_pidsRu  (   RI   R   R=  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG   -  s    							c         C@  s   |  S(   N(    (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   __iter__:  s    c      	   C@  s   |  j   y |  j j   } Wn t k
 r |  j |  j k rQ t |  _ t  n  |  j  j	 |  y |  j j   } Wq t k
 r |  j |  j k r t |  _ t  n  t
  q Xn XWd  QX| \ } } | r | St |   d  S(   N(   RN   R  t   popleftR  R   R  R   R  R  R  R   R   (   RI   RB   R%  R  RJ   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   =  s&    
				c         C@  s   |  j   |  j | k r |  j j |  |  j d 7_ xJ |  j |  j k r |  j j |  j  } |  j j |  |  j d 7_ q; W|  j  j   n | |  j | <|  j |  j k r t |  _	 |  j
 |  j =n  Wd  QXd  S(   Ni   (   RN   R   R  Rl  R  R  RT   R  R   R  R7  R  (   RI   R   R$  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  U  s    
	c         C@  sU   |  j  F | |  _ |  j |  j k rK t |  _ |  j  j   |  j |  j =n  Wd  QXd  S(   N(   RN   R  R   R   R  RT   R7  R  (   RI   R  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  f  s    
		c         G@  s   |  j  j |  d  S(   N(   R  Rl  (   RI   R   R"  R   R5   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR!  n  s    c         C@  s   |  j  S(   N(   R  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR   q  s    c         C@  s   |  j  S(   N(   R  (   RI   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRx  t  s    N(   RY   RZ   R)   Rt  R<  RG   R  R   t   __next__R  R  R!  R   Rx  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  *  s   					R  c           B@  s   e  Z d    Z RS(   c         C@  sk   |  j  \ |  j j |  |  j d 7_ |  j  j   |  j |  j k ra t |  _ |  j |  j	 =n  Wd  QXd  S(   Ni   (
   RN   R  Rl  R   RT   R  R   R  R7  R  (   RI   R   R$  (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  ~  s    
	(   RY   RZ   R  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR  |  s   t
   ThreadPoolc           B@  sH   e  Z d  d l m Z e Z d d d d  Z d   Z e d    Z	 RS(   i    (   R   c         C@  s   t  j |  | | |  d  S(   N(   R2  RG   (   RI   R   Rk   Rl   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRG     s    c         @  sR   t      _ t      _   j j   _   j j   _   f d   } |   _ d  S(   Nc         @  s9   y t    j d |   f SWn t k
 r4 t d  f SXd  S(   NRB   (   R   R   R   R   R)   (   RB   (   RI   (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyRd    s    (   R   R]  RR  R   R   R   R   Rd  (   RI   Rd  (    (   RI   s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR5    s    c         C@  sK   |  j  < |  j j   |  j j d  g t |   |  j  j   Wd  QXd  S(   N(   t	   not_emptyt   queueRS   t   extendR)   R0  RP   (   R  R  R   (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s    
N(    (
   RY   RZ   t   billiard.dummyR   R   R)   RG   R5  R  R  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyR    s
   	(j   t
   __future__R    R   R7   R   R   R#   R   R>   R   RE  t   collectionsR   t	   functoolsR   t    R   R   R   R   t   commonR   R   R	   t   compatR
   R   R   R   t   dummyR   t
   exceptionsR   R   R   R   R   R   R   t   fiveR   R   R   R   R   R   R   R   t   version_infoRE   t   systemt   _winR   R   R   R  R   R  R(   t	   SemaphoreRF   t   dictt   dirR/   R   R   R   R   R   R   R   R   R'   R   R   R"   R)   R   R<  t   countR  RU  R-   R2   R6   R9   R;   RC   RD   R   R\   Rc   Rf   Rg   R   R   R   R   R  t   objectR2  R  R  R  R  R  (    (    (    s/   /tmp/pip-unpacked-wheel-40MfcW/billiard/pool.pyt   <module>
   s   4	.	
			
					;	)%>x  =R