î j f¦D ã @ 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)Ú _shutdownÚlistÚ_threads_queuesÚitemsÚputÚjoin)r ÚtÚq© 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)ÚfutureÚfnÚargsÚkwargs)Ú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Ú exceptionÚresult)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 ÚosÚgetpidr r r Ú BaseExceptionr r )Ú call_queueÚresult_queueZ call_itemÚrÚer 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&