B /g^tmc@sdZdZddlZddlZddlmZddlZddlmZddlZ ddl m Z ddl m Z ddlZddlZddlmZddlZddlZddlZeZd aGd d d Zd d ZdZdZGdddeZGdddZddZGddde Z!Gddde Z"Gddde Z#Gddde Z$ddZ%d d!Z&d2d"d#Z'd$d%Z(d&d'Z)d(d)Z*d a+da,d*d+Z-d,d-Z.Gd.d/d/ej/Z0Gd0d1d1ej1Z2e3edS)3a* Implements ProcessPoolExecutor. The follow diagram and text describe the data-flow through the system: |======================= In-process =====================|== Out-of-process ==| +----------+ +----------+ +--------+ +-----------+ +---------+ | | => | Work Ids | | | | Call Q | | Process | | | +----------+ | | +-----------+ | Pool | | | | ... | | | | ... | +---------+ | | | 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)wait)Queue)partialFc@s,eZdZddZddZddZddZd S) _ThreadWakeupcCstjdd\|_|_dS)NF)Zduplex)mpZPipe_reader_writerselfr 2/usr/lib64/python3.7/concurrent/futures/process.py__init__Rz_ThreadWakeup.__init__cCs|j|jdSN)r closerr r r r rU z_ThreadWakeup.closecCs|jddS)Ns)r Z send_bytesr r r r wakeupYrz_ThreadWakeup.wakeupcCsx|jr|jqWdSr)rZpollZ recv_bytesr r r r clear\ z_ThreadWakeup.clearN)__name__ __module__ __qualname__rrrrr r r r rQsrcCsHdatt}x|D]\}}|qWx|D]\}}|q0WdSNT)_global_shutdownZlist_threads_wakeupsitemsrjoin)r_ thread_wakeupZtr r r _python_exitas   r i=c@eZdZddZddZdS)_RemoteTracebackcCs ||_dSrtb)r r%r r r rzrz_RemoteTraceback.__init__cCs|jSrr$r r r r __str__|rz_RemoteTraceback.__str__N)rrrrr&r r r r r#ysr#c@r")_ExceptionWithTracebackcCs0tt|||}d|}||_d||_dS)Nz """ %s""") tracebackformat_exceptiontyperexcr%)r r,r%r r r rs z _ExceptionWithTraceback.__init__cCst|j|jffSr) _rebuild_excr,r%r r r r __reduce__rz"_ExceptionWithTraceback.__reduce__N)rrrrr.r r r r r'sr'cCst||_|Sr)r# __cause__)r,r%r r r r-rr-c@eZdZddZdS) _WorkItemcC||_||_||_||_dSr)futurefnargskwargs)r r3r4r5r6r r r rz_WorkItem.__init__Nrrrrr r r r r1r1c@seZdZdddZdS) _ResultItemNcCs||_||_||_dSr)work_id exceptionresult)r r;r<r=r r r rsz_ResultItem.__init__NNr8r r r r r:r9r:c@r0) _CallItemcCr2r)r;r4r5r6)r r;r4r5r6r r r rr7z_CallItem.__init__Nr8r r r r r?r9r?cs.eZdZdZdfdd ZfddZZS) _SafeQueuez=Safe Queue set exception to the future object linked to a jobrcs||_tj||ddS)N)ctx)pending_work_itemssuperr)r max_sizerArBZ __class__r r rsz_SafeQueue.__init__cslt|trZtt|||j}tdd||_ |j |j d}|dk rh|j |nt||dS)Nz """ {}"""r() isinstancer?r)r*r+ __traceback__r#Zformatrr/rBpopr;r3 set_exceptionrC_on_queue_feeder_error)r eZobjr% work_itemrEr r rJs z!_SafeQueue._on_queue_feeder_error)r)rrr__doc__rrJ __classcell__r r rEr r@sr@cgs0t|}x"tt||}|s"dS|Vq WdS)z, Iterates over zip()ed iterables in chunks. N)ZzipZtuple itertoolsZislice) chunksize iterablesZitchunkr r r _get_chunkss rScsfdd|DS)z Processes a chunk of an iterable passed to map. Runs the function passed to map() on a chunk of the iterable passed to map. This function is run in a separate process. csg|] }|qSr r ).0r5r4r r z"_process_chunk..r )r4rRr rUr _process_chunks rXc Cs^y|t|||dWn@tk rX}z"t||j}|t||dWdd}~XYnXdS)z.Safely send back the given result or exception)r=r<r<N)putr: BaseExceptionr'rG) result_queuer;r=r<rKr,r r r _sendback_results   r]c Cs|dk r:y ||Wn$tk r8tjjddddSXx|jdd}|dkrb|tdSy|j|j |j }Wn>tk r}z t ||j }t ||j|dWdd}~XYnXt ||j|d~q, 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. NFr^T) Zfullr_queueZEmptyr3Zset_running_or_notify_cancelrZr?r4r5r6)rBZwork_idsrar;rLr r r _add_call_item_to_queues   rfc sRdfdd}fdd}|j} |j} | | g} xt||ddD} t| | } d}d}| | kry| }d }Wqtk r}ztt|||j }Wdd}~XYqXn| | krd }d}| |rt|dk rd _ d_ dt d }|dk r td d |d|_x$|D]\}}|j|~q*W| xD]}|qXW|dSt|tr|st|}|s|dSnL|dk r||jd}|dk r|jr|j|jn|j|j~~||rFy$dk rd_ |s,|dSWntk rDYnXdq6WdS)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 ctx.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 ctx.Queue that will be filled with _CallItems derived from _WorkItems for processing by the process workers. result_queue: A ctx.SimpleQueue of _ResultItems generated by the process workers. thread_wakeup: A _ThreadWakeup to allow waking up the queue_manager_thread from the main Thread and avoid deadlocks caused by permanently locked queues. NcstpdkpjSr)r_shutdown_threadr )executorr r shutting_down?rz/_queue_management_worker..shutting_downc stddD}|}d}xn||kr|dkrxBt||D]2}yd|d7}Wq>tk rnPYq>Xq>WtddD}q WxD] }|qWdS)Ncs|]}|VqdSrZis_aliverTpr r r EzD_queue_management_worker..shutdown_worker..rr!csrjrrkrlr r r rnQro)ZsumvaluesrangeZ put_nowaitrrr)Zn_children_aliveZn_children_to_stopZn_sentinels_sentZirm)ra processesr r shutdown_workerCs   z1_queue_management_worker..shutdown_workercSsg|] }|jqSr )Zsentinelrlr r r rVhrWz,_queue_management_worker..TFzKA child process terminated abruptly, the process pool is not usable anymorez^A process in the process pool was terminated abruptly while the future was running or pending.z ''' r(z''')rrfrprZrecvr[r)r*r+rGr_brokenrgBrokenProcessPoolr#rr/rr3rIZ terminaterFZintZAssertionErrorrHr;r<Z set_resultr=r)Zexecutor_referencerrrBZwork_ids_queuerar\rrirsZ result_readerZ wakeup_readerZreadersZworker_sentinelsZreadyZcauseZ is_brokenZ result_itemrKZbper;rLrmr )rarhrrr _queue_management_worker!s  (        rvc Cshtrtrttdaytd}Wnttfk r:dSX|dkrHdS|dkrTdSd|attdS)NTZSC_SEM_NSEMS_MAXiiz@system provides too few semaphores (%d available, 256 necessary))_system_limits_checked_system_limitedZNotImplementedErrorr`ZsysconfZAttributeError ValueError)Z nsems_maxr r r _check_system_limitssrzccs.x(|D] }|x|r$|VqWqWdS)z Specialized implementation of itertools.chain.from_iterable. Each item in *iterable* should be a list. This function is careful not to keep references to yielded objects. N)ZreverserH)ZiterableZelementr r r _chain_from_iterable_of_listss r{c@seZdZdZdS)ruzy Raised when a process in a ProcessPoolExecutor terminated abruptly while a future was in the running state. N)rrrrMr r r r rusrucsheZdZdddZddZddZd d Zejjj e_ dd d fd d Z dddZ ejj j e _ Z S)ProcessPoolExecutorNr cCst|dkr6tpd|_tjdkrntt|j|_n8|dkrHtdn tjdkrh|tkrhtdt||_|dkr~t }||_ |dk rt |st d||_||_d|_i|_d|_t|_d|_d|_i|_|jt}t||j |jd |_d |j_||_t |_!t"|_#dS) aSInitializes 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. mp_context: A multiprocessing context to launch the workers. This object should provide SimpleQueue, Queue and Process. initializer: A callable used to initialize worker processes. initargs: A tuple of arguments to pass to the initializer. Nr!Zwin32rz"max_workers must be greater than 0zmax_workers must be <= zinitializer must be a callableF)rDrArBT)$rzr`Z cpu_count _max_workerssysZplatformZmin_MAX_WINDOWS_WORKERSryrZ get_context _mp_contextZcallable TypeError _initializer _initargs_queue_management_thread _processesrg threadingZLock_shutdown_lockrt _queue_count_pending_work_itemsEXTRA_QUEUED_CALLSr@ _call_queueZ _ignore_epipeZ SimpleQueue _result_queuerer _work_idsr_queue_management_thread_wakeup)r Z max_workersZ mp_contextrbrcZ queue_sizer r r rsF           zProcessPoolExecutor.__init__c Csv|jdkrr|jfdd}|tjtt|||j|j |j |j |j |jfdd|_d|j_ |j|jt|j<dS)NcSstjd|dS)Nz?Executor collected: triggering callback for QueueManager wakeup)rZutilZdebugr)rrr r r weakref_cbAs zFProcessPoolExecutor._start_queue_management_thread..weakref_cbZQueueManagerThread)targetr5ZnameT)rr_adjust_process_countrZThreadrvweakrefZrefrrrrrZdaemonstartr)r rr r r _start_queue_management_thread<s     z2ProcessPoolExecutor._start_queue_management_threadcCsTxNtt|j|jD]8}|jjt|j|j|j |j fd}| ||j|j <qWdS)N)rr5) rqlenrr}rZProcessrdrrrrrZpid)r rrmr r r rWs z)ProcessPoolExecutor._adjust_process_countc Ost|dkr|^}}}n>|s&tdn0d|krB|d}|^}}ntdt|d|j|jrnt|j|jr|tdtrtdt }t ||||}||j |j <|j|j |j d7_ |j||SQRXdS)NizEdescriptor 'submit' of 'ProcessPoolExecutor' object needs an argumentr4z6submit expected at least 1 positional argument, got %dr!z*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdown)rrrHrrtrurgZ RuntimeErrorrrZFuturer1rrrrZrrr)r5r6r r4ZfZwr r r submitbs0        zProcessPoolExecutor.submitr!)timeoutrPcs:|dkrtdtjtt|t|d|i|d}t|S)ajReturns an iterator equivalent to map(fn, iter). Args: fn: A callable that will take as many arguments as there are passed iterables. timeout: The maximum number of seconds to wait. If None, then there is no limit on the wait time. chunksize: If greater than one, the iterables will be chopped into chunks of size chunksize and submitted to the process pool. If set to one, the items in the list will be sent one at a time. Returns: An iterator equivalent to: map(func, *iterables) but the calls may be evaluated out-of-order. Raises: TimeoutError: If the entire result iterator could not be generated before the given timeout. Exception: If fn(*args) raises for any values. r!zchunksize must be >= 1.rP)r)ryrCmaprrXrSr{)r r4rrPrQZresultsrEr r rs  zProcessPoolExecutor.mapTc Cs|j d|_WdQRX|jr6|j|r6|jd|_|jdk rd|j|r^|jd|_d|_ d|_ |jr|jd|_dSr) rrgrrrrrrZ join_threadrr)r rr r r shutdowns"      zProcessPoolExecutor.shutdown)NNNr )T) rrrrrrrrExecutorrMrrrNr r rEr r|s J !  r|r>)4rMZ __author__Zatexitr`Zconcurrent.futuresrrerZmultiprocessingrZmultiprocessing.connectionrZmultiprocessing.queuesrrrZ functoolsrrOr~r)ZWeakKeyDictionaryrrrr rrZ Exceptionr#r'r-Zobjectr1r:r?r@rSrXr]rdrfrvrwrxrzr{ZBrokenExecutorrurr|Zregisterr r r r Z,sV         (&! L