ó
ÕÜ-]c           @   s!  d  Z  d d l Z 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 Z
 e j ƒ  Z e a d „  Z d Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d „  Z e a d a d „  Z d e j f d „  ƒ  YZ e j e ƒ d S(   s+	  Implements ProcessPoolExecutor.

The follow diagram and text describe the data-flow through the system:

|======================= In-process =====================|== Out-of-process ==|

+----------+     +----------+       +--------+     +-----------+    +---------+
|          |  => | Work Ids |    => |        |  => | Call Q    | => |         |
|          |     +----------+       |        |     +-----------+    |         |
|          |     | ...      |       |        |     | ...       |    |         |
|          |     | 6        |       |        |     | 5, call() |    |         |
|          |     | 7        |       |        |     | ...       |    |         |
| Process  |     | ...      |       | Local  |     +-----------+    | Process |
|  Pool    |     +----------+       | Worker |                      |  #1..n  |
| Executor |                        | Thread |                      |         |
|          |     +----------- +     |        |     +-----------+    |         |
|          | <=> | Work Items | <=> |        | <=  | Result Q  | <= |         |
|          |     +------------+     |        |     +-----------+    |         |
|          |     | 6: call()  |     |        |     | ...       |    |         |
|          |     |    future  |     |        |     | 4, result |    |         |
|          |     | ...        |     |        |     | 3, except |    |         |
+----------+     +------------+     +--------+     +-----------+    +---------+

Executor.submit() called:
- creates a uniquely numbered _WorkItem and adds it to the "Work Items" dict
- adds the id of the _WorkItem to the "Work Ids" queue

Local worker thread:
- reads work ids from the "Work Ids" queue and looks up the corresponding
  WorkItem from the "Work Items" dict: if the work item has been cancelled then
  it is simply removed from the dict, otherwise it is repackaged as a
  _CallItem and put in the "Call Q". New _CallItems are put in the "Call Q"
  until "Call Q" is full. NOTE: the size of the "Call Q" is kept small because
  calls placed in the "Call Q" can no longer be cancelled with Future.cancel().
- reads _ResultItems from "Result Q", updates the future stored in the
  "Work Items" dict and deletes the dict entry

Process #1..n:
- reads _CallItems from "Call Q", executes the calls, and puts the resulting
  _ResultItems in "Request Q"
iÿÿÿÿN(   t   _bases"   Brian Quinlan (brian@sweetapp.com)c          C   ss   t  a t r t t j ƒ  ƒ n d }  x! |  D] \ } } | j d  ƒ q+ Wx$ |  D] \ } } | j t j	 ƒ qO Wd  S(   N(    (
   t   Truet	   _shutdownt   _threads_queuest   listt   itemst   putt   Nonet   joint   syst   maxint(   R   t   tt   q(    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   _python_exitI   s    i   t	   _WorkItemc           B   s   e  Z d  „  Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   futuret   fnt   argst   kwargs(   t   selfR   R   R   R   (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   __init__Y   s    			(   t   __name__t
   __module__R   (    (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyR   X   s   t   _ResultItemc           B   s   e  Z d d d  „ Z RS(   c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   work_idt	   exceptiont   result(   R   R   R   R   (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyR   `   s    		N(   R   R   R   R   (    (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyR   _   s   t	   _CallItemc           B   s   e  Z d  „  Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R   R   R   R   (   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyR   f   s    			(   R   R   R   (    (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyR   e   s   c         C   s®   x§ t  r© |  j d t  ƒ } | d k r8 | j d ƒ d Sy | j | j | j Ž  } Wn3 t j ƒ  d } | j t	 | j
 d | ƒƒ q X| j t	 | j
 d | ƒƒ q Wd S(   sø  Evaluates calls from call_queue and places the results in result_queue.

    This worker is run in a separate process.

    Args:
        call_queue: A multiprocessing.Queue of _CallItems that will be read and
            evaluated by the worker.
        result_queue: A multiprocessing.Queue of _ResultItems that will written
            to by the worker.
        shutdown: A multiprocessing.Event that will be set as a signal to the
            worker that it should exit when call_queue is empty.
    t   blockNi   R   R   (   R   t   getR   R   R   R   R   R	   t   exc_infoR   R   (   t
   call_queuet   result_queuet	   call_itemt   rt   e(    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   _process_workerl   s    	c         C   s    x™ t  r› | j ƒ  r d Sy | j d t ƒ } Wn t j k
 rF d SX|  | } | j j ƒ  rŽ | j t	 | | j
 | j | j ƒ d t  ƒq |  | =q q Wd S(   sM  Fills call_queue with _WorkItems from pending_work_items.

    This function never blocks.

    Args:
        pending_work_items: A dict mapping work ids to _WorkItems e.g.
            {5: <_WorkItem...>, 6: <_WorkItem...>, ...}
        work_ids: A queue.Queue of work ids e.g. Queue([5, 6, ...]). Work ids
            are consumed and the corresponding _WorkItems from
            pending_work_items are transformed into _CallItems and put in
            call_queue.
        call_queue: A multiprocessing.Queue that will be filled with _CallItems
            derived from _WorkItems.
    NR   (   R   t   fullR   t   Falset   queuet   EmptyR   t   set_running_or_notify_cancelR   R   R   R   R   (   t   pending_work_itemst   work_idsR   R   t	   work_item(    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   _add_call_item_to_queue‰   s     	

c            s&  d g ‰ ‡  ‡ f d †  } xt  r!t | | ˆ  ƒ | j d t  ƒ } | d k	 r¡ | | j } | | j =| j rˆ | j j | j ƒ n | j j | j	 ƒ ~ n  |  ƒ  }	 t
 sÅ |	 d k sÅ |	 j r| sx! ˆ d t | ƒ k  rî | ƒ  qÎ Wx | D] }
 |
 j ƒ  qö Wˆ  j ƒ  d Sn  ~	 q Wd S(   s‘  Manages the communication between this process and the worker processes.

    This function is run in a local thread.

    Args:
        executor_reference: A weakref.ref to the ProcessPoolExecutor that owns
            this thread. Used to determine if the ProcessPoolExecutor has been
            garbage collected and that this function can exit.
        process: A list of the multiprocessing.Process instances used as
            workers.
        pending_work_items: A dict mapping work ids to _WorkItems e.g.
            {5: <_WorkItem...>, 6: <_WorkItem...>, ...}
        work_ids_queue: A queue.Queue of work ids e.g. Queue([5, 6, ...]).
        call_queue: A multiprocessing.Queue that will be filled with _CallItems
            derived from _WorkItems for processing by the process workers.
        result_queue: A multiprocessing.Queue of _ResultItems generated by the
            process workers.
    i    c              s!   ˆ  j  d ƒ ˆ d c d 7<d S(   s<   Tell a worker to terminate, which will in turn wake us againi    i   N(   R   R   (    (   R   t   nb_shutdown_processes(    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   shutdown_one_processÇ   s    R   N(   R   R-   R   R   R   R   R   t   set_exceptiont
   set_resultR   R   t   _shutdown_threadt   lenR   t   close(   t   executor_referencet	   processesR*   t   work_ids_queueR   R    R/   t   result_itemR,   t   executort   p(    (   R   R.   s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   _queue_management_worker®   s0    		
		
c          C   s˜   t  r t r t t ƒ ‚ q n  t a  y d d  l }  |  j d ƒ } Wn t t f k
 r] d  SX| d k rn d  S| d k r~ d  Sd | a t t ƒ ‚ d  S(   Niÿÿÿÿt   SC_SEM_NSEMS_MAXi   s@   system provides too few semaphores (%d available, 256 necessary)(   t   _system_limits_checkedt   _system_limitedt   NotImplementedErrorR   t   ost   sysconft   AttributeErrort
   ValueError(   R@   t	   nsems_max(    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   _check_system_limitsò   s    
t   ProcessPoolExecutorc           B   s_   e  Z d d  „ Z d „  Z d „  Z d „  Z e j j j	 e _	 e
 d „ Z e j j j	 e _	 RS(   c         C   sÃ   t  ƒ  | d k r% t j ƒ  |  _ n$ | d k r@ t d ƒ ‚ n  | |  _ t j |  j t ƒ |  _ t j ƒ  |  _	 t
 j ƒ  |  _ d |  _ t ƒ  |  _ t |  _ t j ƒ  |  _ d |  _ i  |  _ d S(   s/  Initializes a new ProcessPoolExecutor instance.

        Args:
            max_workers: The maximum number of processes that can be used to
                execute the given calls. If None or not given then as many
                worker processes will be created as the machine has processors.
        i    s"   max_workers must be greater than 0N(   RE   R   t   multiprocessingt	   cpu_countt   _max_workersRC   t   Queuet   EXTRA_QUEUED_CALLSt   _call_queuet   _result_queueR'   t	   _work_idst   _queue_management_threadt   sett
   _processesR&   R2   t	   threadingt   Lockt   _shutdown_lockt   _queue_countt   _pending_work_items(   R   t   max_workers(    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyR     s     				c      
   C   s–   |  j  d „ } |  j d  k r’ t j d t d t j |  | ƒ |  j |  j	 |  j
 |  j |  j  f ƒ |  _ t |  j _ |  j j ƒ  |  j  t |  j <n  d  S(   Nc         S   s   | j  d  ƒ d  S(   N(   R   R   (   t   _R   (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt
   weakref_cb0  s    t   targetR   (   RM   RO   R   RR   t   ThreadR;   t   weakreft   refRQ   RV   RN   RL   R   t   daemont   startR   (   R   RY   (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   _start_queue_management_thread-  s    	c         C   sh   xa t  t |  j ƒ |  j ƒ D]D } t j d t d |  j |  j f ƒ } | j	 ƒ  |  j j
 | ƒ q Wd  S(   NRZ   R   (   t   rangeR3   RQ   RI   RG   t   ProcessR$   RL   RM   R_   t   add(   R   RX   R:   (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   _adjust_process_count?  s    "	
c      	   O   s§   |  j  ˜ |  j r" t d ƒ ‚ n  t j ƒ  } t | | | | ƒ } | |  j |  j <|  j j	 |  j ƒ |  j d 7_ |  j
 j	 d  ƒ |  j ƒ  |  j ƒ  | SWd  QXd  S(   Ns*   cannot schedule new futures after shutdowni   (   RT   R2   t   RuntimeErrorR    t   FutureR   RV   RU   RN   R   RM   R   R`   Rd   (   R   R   R   R   t   ft   w(    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   submitH  s    
	

c         C   sy   |  j   t |  _ Wd  QX|  j rQ |  j j d  ƒ | rQ |  j j t j	 ƒ qQ n  d  |  _ d  |  _
 d  |  _ d  |  _ d  S(   N(   RT   R   R2   RO   RM   R   R   R   R	   R
   RL   RQ   (   R   t   wait(    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   shutdown[  s    
				N(   R   R   R   R   R`   Rd   Ri   R    t   Executort   __doc__R   Rk   (    (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyRF   
  s   "				(   Rm   t   atexitt   concurrent.futuresR    RJ   R'   RG   RR   R\   R	   t
   __author__t   WeakKeyDictionaryR   R&   R   R   RK   t   objectR   R   R   R$   R-   R;   R=   R   R>   RE   Rl   RF   t   register(    (    (    s9   lib/python2.7/site-packages/concurrent/futures/process.pyt   <module>,   s.   			%	B	a