Your IP : 3.144.95.167


Current Path : /opt/alt/python34/lib64/python3.4/concurrent/futures/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/concurrent/futures/__pycache__/process.cpython-34.pyo

�
j f�D�@svdZdZddlZddlZddlmZddlZddlmZddlZddlm	Z	ddl
mZddlZddl
Z
e
j�Zdad	d
�ZdZGdd
�d
e�ZGdd�de�ZGdd�de�Zdd�Zdd�Zdd�Zdadadd�ZGdd�de�ZGdd�dej�Z ej!e�dS)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)�waitFcCsadattj��}x!|D]\}}|jd�qWx|D]\}}|j�qCWdS)NT)�	_shutdown�list�_threads_queues�items�put�join)r	�t�q�r�?/opt/alt/python34/lib64/python3.4/concurrent/futures/process.py�_python_exitLsr�c@seZdZdd�ZdS)�	_WorkItemcCs(||_||_||_||_dS)N)�future�fn�args�kwargs)�selfrrrrrrr�__init__\s			z_WorkItem.__init__N)�__name__�
__module__�__qualname__rrrrrr[src@s"eZdZdddd�ZdS)�_ResultItemNcCs||_||_||_dS)N)�work_id�	exception�result)rrrrrrrrcs		z_ResultItem.__init__)rrrrrrrrrbsrc@seZdZdd�ZdS)�	_CallItemcCs(||_||_||_||_dS)N)rrrr)rrrrrrrrris			z_CallItem.__init__N)rrrrrrrrr hsr cCs�x�|jdd�}|dkr8|jtj��dSy|j|j|j�}WnAtk
r�}z!|jt|j	d|��WYdd}~XqX|jt|j	d|��qWdS)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.
    �blockTNrr)
�getr
�os�getpidrrr�
BaseExceptionrr)�
call_queue�result_queueZ	call_item�r�errr�_process_workeros
r*cCs�x�|j�rdSy|jdd�}Wntjk
rDdSYqX||}|jj�r�|jt||j|j	|j
�dd�q||=qqWdS)aMFills 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"�queueZEmptyrZset_running_or_notify_cancelr
r rrr)�pending_work_itemsZwork_idsr&r�	work_itemrrr�_add_call_item_to_queue�s 	

r/csd��fdd�}��fdd�}|j}x�t||��dd��j�D�}	t|g|	�}
||
kr�|j�}n�|���dk	r�d�_d�_d�nx3|j�D]%\}}
|
jj	t
d	��~
q�W|j�x�j�D]}|j�q
W|�dSt
|t�rh�j|�}|j��s�|�dSnh|dk	r�|j|jd�}
|
dk	r�|jr�|
jj	|j�n|
jj|j�~
q�n|��|�ry|s�|�dSWqtk
rYqXnd�q9WdS)
a�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.
    Ncstp�dkp�jS)N)r�_shutdown_threadr)�executorrr�
shutting_down�sz/_queue_management_worker.<locals>.shutting_downcsutdd��j�D��}x$td|�D]}�jd�q/W�j�x�j�D]}|j�q]WdS)Ncss|]}|j�VqdS)N)Zis_alive)�.0�prrr�	<genexpr>�szD_queue_management_worker.<locals>.shutdown_worker.<locals>.<genexpr>r)�sum�values�rangeZ
put_nowait�closer)Znb_children_alive�ir4)r&�	processesrr�shutdown_worker�s
z1_queue_management_worker.<locals>.shutdown_workercSsg|]}|j�qSr)�sentinel)r3r4rrr�
<listcomp>�s	z,_queue_management_worker.<locals>.<listcomp>Tz^A process in the process pool was terminated abruptly while the future was running or pending.)Z_readerr/r7rZrecv�_brokenr0r	rZ
set_exception�BrokenProcessPool�clearZ	terminate�
isinstance�int�poprrrZ
set_resultrr)Zexecutor_referencer;r-Zwork_ids_queuer&r'r2r<�readerZ	sentinelsZreadyZresult_itemrr.r4r)r&r1r;r�_queue_management_worker�sb						


				
rFcCs�trtrtt��qndaytjd�}Wnttfk
rUdSYnX|dkrfdS|dkrvdSd|att��dS)NT�SC_SEM_NSEMS_MAXr�z@system provides too few semaphores (%d available, 256 necessary)���)�_system_limits_checked�_system_limited�NotImplementedErrorr#�sysconf�AttributeError�
ValueError)Z	nsems_maxrrr�_check_system_limits%s	
rPc@seZdZdZdS)r@zy
    Raised when a process in a ProcessPoolExecutor terminated abruptly
    while a future was in the running state.
    N)rrr�__doc__rrrrr@<sr@c@sveZdZddd�Zdd�Zdd�Zdd	�Zejjj	e_	d
dd�Z
ejj
j	e
_	dS)
�ProcessPoolExecutorNcCs�t�|dkr+tj�p"d|_n	||_tj|jt�|_d|j_t	�|_
tj�|_d|_
i|_d|_tj�|_d|_d|_i|_dS)a/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.
        NrTFr)rPr#�	cpu_count�_max_workers�multiprocessingZQueue�EXTRA_QUEUED_CALLS�_call_queueZ
_ignore_epiper�
_result_queuer,�	_work_ids�_queue_management_thread�
_processesr0�	threadingZLock�_shutdown_lockr?�_queue_count�_pending_work_items)rZmax_workersrrrrDs 	
					zProcessPoolExecutor.__init__c
Cs�|jdd�}|jdkr�|j�tjdtdtj||�|j|j	|j
|j|jf�|_d|j_|jj
�|jt|j<ndS)NcSs|jd�dS)N)r
)�_r
rrr�
weakref_cblszFProcessPoolExecutor._start_queue_management_thread.<locals>.weakref_cb�targetrT)rXrZ�_adjust_process_countr\ZThreadrF�weakref�refr[r_rYrWZdaemon�startr)rrarrr�_start_queue_management_threadis
	
z2ProcessPoolExecutor._start_queue_management_threadcCshxatt|j�|j�D]D}tjdtd|j|jf�}|j	�||j|j
<qWdS)Nrbr)r8�lenr[rTrUZProcessr*rWrXrf�pid)rr`r4rrrrc}s"	
z)ProcessPoolExecutor._adjust_process_countcOs�|j��|jr"td��n|jr:td��ntj�}t||||�}||j|j	<|j
j|j	�|j	d7_	|jjd�|j
�|SWdQXdS)NzKA child process terminated abruptly, the process pool is not usable anymorez*cannot schedule new futures after shutdownr)r]r?r@r0�RuntimeErrorrZFuturerr_r^rYr
rXrg)rrrr�f�wrrr�submit�s
		
zProcessPoolExecutor.submitTc	Css|j�d|_WdQX|jrK|jjd�|rK|jj�qKnd|_d|_d|_d|_dS)NT)r]r0rZrXr
rrWr[)rrrrr�shutdown�s
				zProcessPoolExecutor.shutdown)rrrrrgrcrmr�ExecutorrQrnrrrrrRCs%	rR)"rQ�
__author__�atexitr#Zconcurrent.futuresrr,rrUrZmultiprocessing.connectionrr\rd�WeakKeyDictionaryrrrrV�objectrrr r*r/rFrJrKrPrjr@rorR�registerrrrr�<module>,s6
%sh

?>