j fD @ sv d Z d Z d d l Z d d l Z d d l m Z d d l Z d d l m Z d d l Z d d l m Z d d l m Z d d l Z d d l Z e j Z d a d d Z d Z Gd d d e Z Gd d d e Z Gd d d e Z d d Z d d Z d d Z d a d a d d Z Gd d d e Z Gd d d e j Z e j! e d S)a* 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 "Result Q" z"Brian Quinlan (brian@sweetapp.com) N)_base)Full)SimpleQueue)waitFc C sa d a t t j } x! | D] \ } } | j d q Wx | D] \ } } | j qC Wd S)NT) _shutdownlist_threads_queuesitemsputjoin)r tq r ?/opt/alt/python34/lib64/python3.4/concurrent/futures/process.py_python_exitL s r c @ s e Z d Z d d Z d S) _WorkItemc C s( | | _ | | _ | | _ | | _ d S)N)futurefnargskwargs)selfr r r r r r r __init__\ s z_WorkItem.__init__N)__name__ __module____qualname__r r r r r r [ s r c @ s" e Z d Z d d d d Z d S)_ResultItemNc C s | | _ | | _ | | _ d S)N)work_id exceptionresult)r r r r r r r r c s z_ResultItem.__init__)r r r r r r r r r b s r c @ s e Z d Z d d Z d S) _CallItemc C s( | | _ | | _ | | _ | | _ d S)N)r r r r )r r r r r r r r r i s z_CallItem.__init__N)r r r r r r r r r h s r c C s x | j d d } | d k r8 | j t j d Sy | j | j | j } WnA t k r } z! | j t | j d | WYd d } ~ Xq X| j t | j d | q Wd S)a 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. blockTNr r ) getr osgetpidr r r BaseExceptionr r ) call_queueresult_queueZ call_itemrer r r _process_workero s r* c C s x | j r d Sy | j d d } Wn t j k rD d SYq X| | } | j j r | j t | | j | j | j d d q | | =q q Wd S)aM 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! FT)fullr" queueZEmptyr Zset_running_or_notify_cancelr r r r r )pending_work_itemsZwork_idsr&