Current File : //usr/lib/python2.7/site-packages/concurrent/futures/process.pyc
�
i��Xc@s!dZddlZddlmZddlZddlZddlZddlZddl	Z	dZ
ej�Ze
ad�ZdZdefd��YZd	efd
��YZdefd��YZd
�Zd�Zd�Ze
adad�Zdejfd��YZeje�dS(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)cCsstatrttj��nd}x!|D]\}}|jd�q+Wx$|D]\}}|jtj	�qOWdS(N((
tTruet	_shutdownt_threads_queuestlisttitemstputtNonetjointsystmaxint(Rtttq((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt_python_exitIsit	_WorkItemcBseZd�ZRS(cCs(||_||_||_||_dS(N(tfuturetfntargstkwargs(tselfRRRR((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt__init__Ys			(t__name__t
__module__R(((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyRXst_ResultItemcBseZddd�ZRS(cCs||_||_||_dS(N(twork_idt	exceptiontresult(RRRR((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyR`s		N(RRRR(((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyR_st	_CallItemcBseZd�ZRS(cCs(||_||_||_||_dS(N(RRRR(RRRRR((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyRfs			(RRR(((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyRescCs�x�tr�|jdt�}|dkr8|jd�dSy|j|j|j�}Wn3tj�d}|jt	|j
d|��qX|jt	|j
d|��qWdS(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.
    tblockNiRR(RtgetRRRRRR	texc_infoRR(t
call_queuetresult_queuet	call_itemtrte((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt_process_workerls
	
cCs�x�tr�|j�rdSy|jdt�}Wntjk
rFdSX||}|jj�r�|jt	||j
|j|j�dt�q||=qqWdS(sMFills 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(
RtfullRtFalsetqueuetEmptyRtset_running_or_notify_cancelRRRRR(tpending_work_itemstwork_idsRRt	work_item((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt_add_call_item_to_queue�s 	

cs&dg���fd�}xtr!t||��|jdt�}|dk	r�||j}||j=|jr�|jj|j�n|jj|j	�~n|�}	t
s�|	dks�|	jr|sx!�dt|�kr�|�q�Wx|D]}
|
j
�q�W�j�dSn~	qWdS(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.
    ics!�jd��dcd7<dS(s<Tell a worker to terminate, which will in turn wake us againiiN(RR((Rtnb_shutdown_processes(s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pytshutdown_one_process�s
RN(RR-RRRRRt
set_exceptiont
set_resultRRt_shutdown_threadtlenRtclose(texecutor_referencet	processesR*twork_ids_queueRR R/tresult_itemR,texecutortp((RR.s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt_queue_management_worker�s0		

		

cCs�trtrtt��qntayddl}|jd�}Wnttfk
r]dSX|dkrndS|dkr~dSd|att��dS(Ni����tSC_SEM_NSEMS_MAXis@system provides too few semaphores (%d available, 256 necessary)(t_system_limits_checkedt_system_limitedtNotImplementedErrorRtostsysconftAttributeErrort
ValueError(R@t	nsems_max((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt_check_system_limits�s
tProcessPoolExecutorcBs_eZdd�Zd�Zd�Zd�Zejjj	e_	e
d�Zejjj	e_	RS(cCs�t�|dkr%tj�|_n$|dkr@td��n||_tj|jt�|_tj�|_	t
j�|_d|_t
�|_t|_tj�|_d|_i|_dS(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.
        is"max_workers must be greater than 0N(RERtmultiprocessingt	cpu_countt_max_workersRCtQueuetEXTRA_QUEUED_CALLSt_call_queuet
_result_queueR't	_work_idst_queue_management_threadtsett
_processesR&R2t	threadingtLockt_shutdown_lockt_queue_countt_pending_work_items(Rtmax_workers((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyRs 	
			c
Cs�|jd�}|jdkr�tjdtdtj||�|j|j	|j
|j|jf�|_t|j_
|jj�|jt|j<ndS(NcSs|jd�dS(N(RR(t_R((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt
weakref_cb0sttargetR(RMRORRRtThreadR;tweakreftrefRQRVRNRLRtdaemontstartR(RRY((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt_start_queue_management_thread-s	
cCshxatt|j�|j�D]D}tjdtd|j|jf�}|j	�|jj
|�qWdS(NRZR(trangeR3RQRIRGtProcessR$RLRMR_tadd(RRXR:((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt_adjust_process_count?s"	
c	Os�|j��|jr"td��ntj�}t||||�}||j|j<|jj	|j�|jd7_|j
j	d�|j�|j
�|SWdQXdS(Ns*cannot schedule new futures after shutdowni(RTR2tRuntimeErrorRtFutureRRVRURNRRMRR`Rd(RRRRtftw((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pytsubmitHs
	

cCsy|j�t|_WdQX|jrQ|jjd�|rQ|jjtj	�qQnd|_d|_
d|_d|_dS(N(RTRR2RORMRRRR	R
RLRQ(Rtwait((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pytshutdown[s
				N(RRRRR`RdRiRtExecutort__doc__RRk(((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyRF
s"				(Rmtatexittconcurrent.futuresRRJR'RGRRR\R	t
__author__tWeakKeyDictionaryRR&RR
RKtobjectRRRR$R-R;R=RR>RERlRFtregister(((s>/usr/lib/python2.7/site-packages/concurrent/futures/process.pyt<module>,s.	
		%	B	a