Your IP : 3.137.200.56


Current Path : /opt/alt/python34/lib64/python3.4/multiprocessing/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/multiprocessing/__pycache__/pool.cpython-34.pyo

�
e f�a�@s�ddgZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddlm
Z
mZdZdZ
dZej�Zdd	�Zd
d�ZGdd
�d
e�ZGdd�d�Zdd�ZGdd�de�Zdfdddd�ZGdd�de�ZGdd�de�ZeZGdd�de�ZGdd�de�ZGdd�de�ZGd d�de�Z dS)!�Pool�
ThreadPool�N�)�util)�get_context�TimeoutError�cCstt|��S)N)�list�map)�args�r�9/opt/alt/python34/lib64/python3.4/multiprocessing/pool.py�mapstar+srcCsttj|d|d��S)Nrr)r	�	itertools�starmap)rrrr
�starmapstar.src@s(eZdZdd�Zdd�ZdS)�RemoteTracebackcCs
||_dS)N)�tb)�selfrrrr
�__init__6szRemoteTraceback.__init__cCs|jS)N)r)rrrr
�__str__8szRemoteTraceback.__str__N)�__name__�
__module__�__qualname__rrrrrr
r5src@s(eZdZdd�Zdd�ZdS)�ExceptionWithTracebackcCsDtjt|�||�}dj|�}||_d||_dS)N�z

"""
%s""")�	traceback�format_exception�type�join�excr)rr rrrr
r<s	zExceptionWithTraceback.__init__cCst|j|jffS)N)�rebuild_excr r)rrrr
�
__reduce__Asz!ExceptionWithTraceback.__reduce__N)rrrrr"rrrr
r;srcCst|�|_|S)N)r�	__cause__)r rrrr
r!Dsr!cs@eZdZdZ�fdd�Zdd�Zdd�Z�S)�MaybeEncodingErrorzVWraps possible unpickleable errors, so they can be
    safely sent through the socket.csAt|�|_t|�|_tt|�j|j|j�dS)N)�reprr �value�superr$r)rr r&)�	__class__rr
rPszMaybeEncodingError.__init__cCsd|j|jfS)Nz(Error sending result: '%s'. Reason: '%s')r&r )rrrr
rUs	zMaybeEncodingError.__str__cCsdt|�S)Nz<MaybeEncodingError: %s>)�str)rrrr
�__repr__YszMaybeEncodingError.__repr__)rrr�__doc__rrr*rr)r(r
r$Lsr$Fc'Cs�|j}|j}t|d�r>|jj�|jj�n|dk	rW||�nd}xx|dks~|r�||kr�y
|�}	Wn&ttfk
r�tj	d�PYnX|	dkr�tj	d�Pn|	\}
}}}
}yd||
|�f}WnLt
k
rM}z,|r/t||j�}nd|f}WYdd}~XnXy||
||f�Wnbt
k
r�}zBt
||d�}tj	d|�||
|d|ff�WYdd}~XnX|d7}q`Wtj	d	|�dS)
N�_writerrz)worker got EOFError or OSError -- exitingzworker got sentinel -- exitingTFrz0Possible encoding error while sending result: %szworker exiting after %d tasks)�put�get�hasattrr,�close�_reader�EOFError�OSErrorr�debug�	Exceptionr�
__traceback__r$)�inqueue�outqueue�initializer�initargsZmaxtasksZwrap_exceptionr-r.Z	completed�task�job�i�funcr�kwds�result�e�wrappedrrr
�worker]sB		

!


	,rCc@s�eZdZdZdZdd�Zddfdddd�Zdd	�Zd
d�Zdd
�Z	dd�Z
fidd�Zddd�Zddd�Z
ddddd�Zddd�Zddd�Zfidddd�Zddddd �Zdddd!d"�Zed#d$��Zed%d&��Zed'd(��Zed)d*��Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zed3d4��Zed5d6��Zd7d8�Z d9d:�Z!dS);rzS
    Class which supports an async version of applying functions to arguments.
    TcOs|jj||�S)N)�_ctx�Process)rrr?rrr
rE�szPool.ProcessNcCs,|pt�|_|j�tj�|_i|_t|_||_	||_
||_|dkrytj
�psd}n|dkr�td��n|dk	r�t|�r�td��n||_g|_|j�tjdtjd|f�|_d|j_t|j_|jj�tjdtjd|j|j|j|j|jf�|_d|j_t|j_|jj�tjdtjd|j|j |jf�|_!d|j!_t|j!_|j!j�t"j#||j$d|j|j%|j|j|j|j|j!|jfdd�|_&dS)	Nrz&Number of processes must be at least 1zinitializer must be a callable�targetrTZexitpriority�)'rrD�
_setup_queues�queue�Queue�
_taskqueue�_cache�RUN�_state�_maxtasksperchild�_initializer�	_initargs�os�	cpu_count�
ValueError�callable�	TypeError�
_processes�_pool�_repopulate_pool�	threadingZThreadr�_handle_workers�_worker_handler�daemon�start�
_handle_tasks�
_quick_put�	_outqueue�
_task_handler�_handle_results�
_quick_get�_result_handlerrZFinalize�_terminate_pool�_inqueue�
_terminate)r�	processesr9r:Zmaxtasksperchild�contextrrr
r�sT
							
		
		
		
z
Pool.__init__cCszd}xmttt|j���D]P}|j|}|jdk	r"tjd|�|j�d}|j|=q"q"W|S)z�Cleanup after any worker processes which have exited due to reaching
        their specified lifetime.  Returns True if any workers were cleaned up.
        FNzcleaning up worker %dT)�reversed�range�lenrX�exitcoderr4r)rZcleanedr=rCrrr
�_join_exited_workers�s"

zPool._join_exited_workerscCs�x�t|jt|j��D]�}|jdtd|j|j|j|j	|j
|jf�}|jj|�|j
jdd�|_
d|_|j�tjd�qWdS)z�Bring the number of pool processes up to the specified number,
        for use after reaping workers which have exited.
        rFrrEZ
PoolWorkerTzadded workerN)rlrWrmrXrErCrgrarPrQrO�_wrap_exception�append�name�replacer]r^rr4)rr=�wrrr
rY�s#	
zPool._repopulate_poolcCs|j�r|j�ndS)zEClean up any exited workers and start replacements for them.
        N)rorY)rrrr
�_maintain_pool�szPool._maintain_poolcCsL|jj�|_|jj�|_|jjj|_|jjj|_	dS)N)
rDZSimpleQueuergrar,�sendr`r1�recvrd)rrrr
rH�szPool._setup_queuescCs|j|||�j�S)z6
        Equivalent of `func(*args, **kwds)`.
        )�apply_asyncr.)rr>rr?rrr
�apply�sz
Pool.applycCs|j||t|�j�S)zx
        Apply `func` to each element in `iterable`, collecting the results
        in a list that is returned.
        )�
_map_asyncrr.)rr>�iterable�	chunksizerrr
r
�szPool.mapcCs|j||t|�j�S)z�
        Like `map()` method but the elements of the `iterable` are expected to
        be iterables as well and will be unpacked as arguments. Hence
        `func` and (a, b) becomes func(a, b).
        )rzrr.)rr>r{r|rrr
rszPool.starmapcCs|j||t|||�S)z=
        Asynchronous version of `starmap()` method.
        )rzr)rr>r{r|�callback�error_callbackrrr
�
starmap_asyncszPool.starmap_asyncrcs�|jtkrtd��n|dkrrt|j��|jj��fdd�t|�D��jf��St	j
�||�}t|j��|jj�fdd�t|�D��jf�dd��D�SdS)zP
        Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
        zPool not runningrc3s0|]&\}}�j|�|fifVqdS)N)�_job)�.0r=�x)r>r@rr
�	<genexpr>szPool.imap.<locals>.<genexpr>c3s0|]&\}}�j|t|fifVqdS)N)r�r)r�r=r�)r@rr
r�%scss"|]}|D]}|Vq
qdS)Nr)r��chunk�itemrrr
r�'sN)rNrMrT�IMapIteratorrLrKr-�	enumerate�_set_lengthr�
_get_tasks)rr>r{r|�task_batchesr)r>r@r
�imapsz	Pool.imapcs�|jtkrtd��n|dkrrt|j��|jj��fdd�t|�D��jf��St	j
�||�}t|j��|jj�fdd�t|�D��jf�dd��D�SdS)zL
        Like `imap()` method but ordering of results is arbitrary.
        zPool not runningrc3s0|]&\}}�j|�|fifVqdS)N)r�)r�r=r�)r>r@rr
r�1sz&Pool.imap_unordered.<locals>.<genexpr>c3s0|]&\}}�j|t|fifVqdS)N)r�r)r�r=r�)r@rr
r�8scss"|]}|D]}|Vq
qdS)Nr)r�r�r�rrr
r�:sN)rNrMrT�IMapUnorderedIteratorrLrKr-r�r�rr�)rr>r{r|r�r)r>r@r
�imap_unordered)szPool.imap_unorderedcCsb|jtkrtd��nt|j||�}|jj|jd|||fgdf�|S)z;
        Asynchronous version of `apply()` method.
        zPool not runningN)rNrMrT�ApplyResultrLrKr-r�)rr>rr?r}r~r@rrr
rx<s
+zPool.apply_asynccCs|j||t|||�S)z9
        Asynchronous version of `map()` method.
        )rzr)rr>r{r|r}r~rrr
�	map_asyncGszPool.map_asyncc	s|jtkrtd��nt|d�s<t|�}n|dkr�tt|�t|j�d�\}}|r�|d7}q�nt|�dkr�d}ntj	|||�}t
|j|t|�|d|��|jj
��fdd	�t|�D�df��S)
zY
        Helper function to implement map, starmap and their async counterparts.
        zPool not running�__len__N�rrr~c3s0|]&\}}�j|�|fifVqdS)N)r�)r�r=r�)�mapperr@rr
r�csz"Pool._map_async.<locals>.<genexpr>)rNrMrTr/r	�divmodrmrXrr��	MapResultrLrKr-r�)	rr>r{r�r|r}r~Zextrar�r)r�r@r
rzOs (		zPool._map_asynccCsrtj�}xB|jtks6|jrP|jtkrP|j�tjd�qW|j	j
d�tjd�dS)Ng�������?zworker handler exiting)
rZ�current_threadrNrMrL�	TERMINATEru�time�sleeprKr-rr4)�pool�threadrrr
r[gs*
zPool._handle_workerscCs>tj�}x�t|jd�D]�\}}d}d}	y�x�t|�D]�\}	}|jrmtjd�Pny||�WqGtk
r�}
zN|dd�\}}y||j	|d|
f�Wnt
k
r�YnXWYdd}
~
XqGXqGW|rtjd�||	d�nwPWqtk
r�}
zx|rD|dd�nd
\}}||kr}||j	|dd|
f�n|r�tjd�||	d�nWYdd}
~
XqXqWtjd�yFtjd�|jd�tjd	�x|D]}|d�q�WWntk
r,tjd
�YnXtjd�dS)Nrz'task handler found thread._state != RUNrFzdoing set_length()rztask handler got sentinelz/task handler sending sentinel to result handlerz(task handler sending sentinel to workersz/task handler got OSError when sending sentinelsztask handler exiting���)rr)
rZr��iterr.r�rNrr4r5�_set�KeyErrorr-r3)�	taskqueuer-r8r��cacher�ZtaskseqZ
set_lengthr;r=rAr<ZindZex�prrr
r_tsN	


"!
(





zPool._handle_taskscCs�tj�}x�y
|�}Wn)ttfk
rGtjd�dSYnX|jrbtjd�Pn|dkrtjd�Pn|\}}}y||j||�Wqtk
r�YqXqWx�|rn|jt	krny
|�}Wn)ttfk
rtjd�dSYnX|dkr/tjd�q�n|\}}}y||j||�Wq�tk
rjYq�Xq�Wt
|d�r�tjd�y5x.td�D] }|jj
�s�Pn|�q�WWq�ttfk
r�Yq�Xntjdt|�|j�dS)	Nz.result handler got EOFError/OSError -- exitingz,result handler found thread._state=TERMINATEzresult handler got sentinelz&result handler ignoring extra sentinelr1z"ensuring that outqueue is not full�
z7result handler exiting: len(cache)=%s, thread._state=%s)rZr�r3r2rr4rNr�r�r�r/rlr1�pollrm)r8r.r�r�r;r<r=�objrrr
rc�sV

		


	

	

	
	zPool._handle_resultsccsDt|�}x1ttj||��}|s1dS||fVqWdS)N)r��tupler�islice)r>�it�sizer�rrr
r��szPool._get_taskscCstd��dS)Nz:pool objects cannot be passed between processes or pickled)�NotImplementedError)rrrr
r"�szPool.__reduce__cCs8tjd�|jtkr4t|_t|j_ndS)Nzclosing pool)rr4rNrM�CLOSEr\)rrrr
r0�s
	z
Pool.closecCs0tjd�t|_t|j_|j�dS)Nzterminating pool)rr4r�rNr\rh)rrrr
�	terminate�s
	zPool.terminatecCsVtjd�|jj�|jj�|jj�x|jD]}|j�q>WdS)Nzjoining pool)rr4r\rrbrerX)rr�rrr
r�s



z	Pool.joincCsZtjd�|jj�x9|j�rU|jj�rU|jj�tj	d�qWdS)Nz7removing tasks from inqueue until task handler finishedr)
rr4Z_rlock�acquire�is_aliver1r�rwr�r�)r7�task_handlerr�rrr
�_help_stuff_finishs



zPool._help_stuff_finishc	
Cs�tjd�t|_t|_tjd�|j||t|��t|_|jd�tjd�tj�|k	r�|j	�n|r�t
|dd�r�tjd�x-|D]"}	|	jdkr�|	j�q�q�Wntjd�tj�|k	r	|j	�ntjd�tj�|k	r5|j	�n|r�t
|dd�r�tjd	�x>|D]3}	|	j
�rbtjd
|	j�|	j	�qbqbWndS)Nzfinalizing poolz&helping task handler/workers to finishzjoining worker handlerrr�zterminating workerszjoining task handlerzjoining result handlerzjoining pool workerszcleaning up worker %d)rr4r�rNr�rmr-rZr�rr/rnr�r��pid)
�clsr�r7r8r�Zworker_handlerr�Zresult_handlerr�r�rrr
rf
s6
		
	










zPool._terminate_poolcCs|S)Nr)rrrr
�	__enter__;szPool.__enter__cCs|j�dS)N)r�)r�exc_typeZexc_valZexc_tbrrr
�__exit__>sz
Pool.__exit__)"rrrr+rprErrorYrurHryr
rrr�r�rxr�rz�staticmethodr[r_rcr�r"r0r�rr��classmethodrfr�r�rrrr
r�sD	8	

3:			.c@s^eZdZdd�Zdd�Zdd�Zddd	�Zdd
d�Zdd
�ZdS)r�cCsJtj�|_tt�|_||_||_||_|||j<dS)N)	rZZEvent�_event�next�job_counterr�rL�	_callback�_error_callback)rr�r}r~rrr
rGs			zApplyResult.__init__cCs
|jj�S)N)r�Zis_set)rrrr
�readyOszApplyResult.readycCs|jS)N)�_success)rrrr
�
successfulRszApplyResult.successfulNcCs|jj|�dS)N)r��wait)r�timeoutrrr
r�VszApplyResult.waitcCs?|j|�|j�s"t�n|jr2|jS|j�dS)N)r�r�rr��_value)rr�rrr
r.Ys
		zApplyResult.getcCs{|\|_|_|jr7|jr7|j|j�n|jr]|jr]|j|j�n|jj�|j|j=dS)N)r�r�r�r�r��setrLr�)rr=r�rrr
r�bs
zApplyResult._set)	rrrrr�r�r�r.r�rrrr
r�Es	r�c@s(eZdZdd�Zdd�ZdS)r�cCs�tj|||d|�d|_dg||_||_|dkrjd|_|jj�||j=n||t	||�|_dS)Nr~Tr)
r�rr�r��
_chunksize�_number_leftr�r�r��bool)rr�r|�lengthr}r~rrr
rss			

zMapResult.__init__cCs�|\}}|r�||j||j|d|j�<|jd8_|jdkr�|jrq|j|j�n|j|j=|jj�q�nHd|_||_|j	r�|j	|j�n|j|j=|jj�dS)NrrF)
r�r�r�r�rLr�r�r�r�r�)rr=Zsuccess_result�successr@rrr
r��s%	
			
zMapResult._setN)rrrrr�rrrr
r�qs
r�c@sUeZdZdd�Zdd�Zddd�ZeZdd	�Zd
d�ZdS)r�cCsktjtj��|_tt�|_||_tj	�|_
d|_d|_i|_
|||j<dS)Nr)rZZ	ConditionZLock�_condr�r�r�rL�collections�deque�_items�_index�_length�	_unsorted)rr�rrr
r�s				zIMapIterator.__init__cCs|S)Nr)rrrr
�__iter__�szIMapIterator.__iter__NcCs�|jj�z�y|jj�}Wn�tk
r�|j|jkrNt�n|jj|�y|jj�}Wn3tk
r�|j|jkr�t�nt	�YnXYnXWd|jj
�X|\}}|r�|S|�dS)N)r�r�r��popleft�
IndexErrorr�r��
StopIterationr�r�release)rr�r�r�r&rrr
r��s&

	
	zIMapIterator.nextc
Cs�|jj�z�|j|kr�|jj|�|jd7_xJ|j|jkr�|jj|j�}|jj|�|jd7_qAW|jj�n
||j|<|j|jkr�|j	|j
=nWd|jj�XdS)Nr)r�r�r�r�rqr��pop�notifyr�rLr�r�)rr=r�rrr
r��s

zIMapIterator._setc
Cs^|jj�z<||_|j|jkrH|jj�|j|j=nWd|jj�XdS)N)r�r�r�r�r�rLr�r�)rr�rrr
r��s
	
zIMapIterator._set_length)	rrrrr�r��__next__r�r�rrrr
r��s
r�c@seZdZdd�ZdS)r�c
Cst|jj�zR|jj|�|jd7_|jj�|j|jkr^|j|j=nWd|jj	�XdS)Nr)
r�r�r�rqr�r�r�rLr�r�)rr=r�rrr
r��s

zIMapUnorderedIterator._setN)rrrr�rrrr
r��sr�c@s[eZdZdZedd��Zddfdd�Zdd�Zed	d
��ZdS)rFcOsddlm}|||�S)Nr)rE)ZdummyrE)rr?rErrr
rE�szThreadPool.ProcessNcCstj||||�dS)N)rr)rrir9r:rrr
r�szThreadPool.__init__cCs@tj�|_tj�|_|jj|_|jj|_dS)N)rIrJrgrar-r`r.rd)rrrr
rH�szThreadPool._setup_queuesc
CsW|jj�z5|jj�|jjdg|�|jj�Wd|jj�XdS)N)Z	not_emptyr�rI�clear�extendZ
notify_allr�)r7r�r�rrr
r�s

zThreadPool._help_stuff_finish)	rrrrpr�rErrHr�rrrr
r�s
)!�__all__rZrIrr�rRr�rrrrrrMr�r��countr�rrr5rrr!r$rC�objectrr�ZAsyncResultr�r�r�rrrrr
�<module>
s:		,��&%I

?>