B dR@s ddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z ddl mZmZeeZGdddZGd d d ZGd d d eZGd ddeZGdddZGdddZGdddZGdddZGdddeZGdddZeddgZedZedZdS)N) namedtuple)futures)MAXINT)CancelledErrorTransferNotDoneError)FunctionContainer TaskSemaphorec@s0eZdZeddZddZddZddZd S) BaseTransferFuturecCs tddS)z-The metadata associated to the TransferFuturemetaN)NotImplementedError)selfr 9/tmp/pip-target-jj3kjtpb/lib/python/s3transfer/futures.pyr szBaseTransferFuture.metacCs tddS)zmDetermines if a TransferFuture has completed :returns: True if completed. False, otherwise. zdone()N)r )r r r rdone!szBaseTransferFuture.donecCs tddS)zWaits until TransferFuture is done and returns the result If the TransferFuture succeeded, it will return the result. If the TransferFuture failed, it will raise the exception associated to the failure. zresult()N)r )r r r rresult(szBaseTransferFuture.resultcCs tddS)z6Cancels the request associated with the TransferFuturezcancel()N)r )r r r rcancel1szBaseTransferFuture.cancelN)__name__ __module__ __qualname__propertyr rrrr r r rr s  r c@s0eZdZeddZeddZeddZdS)BaseTransferMetacCs tddS)z*The call args used in the transfer request call_argsN)r )r r r rr7szBaseTransferMeta.call_argscCs tddS)zThe unique id of the transfer transfer_idN)r )r r r rr<szBaseTransferMeta.transfer_idcCs tddS)z.A dictionary that requesters can store data in user_contextN)r )r r r rrAszBaseTransferMeta.user_contextN)rrrrrrrr r r rr6s  rc@sBeZdZdddZeddZddZdd Zd d Zd d Z dS)TransferFutureNcCs0||_|dkrt|_||_|dkr,t|_dS)aThe future associated to a submitted transfer request :type meta: TransferMeta :param meta: The metadata associated to the request. This object is visible to the requester. :type coordinator: TransferCoordinator :param coordinator: The coordinator associated to the request. This object is not visible to the requester. N)_meta TransferMeta _coordinatorTransferCoordinator)r r Z coordinatorr r r__init__Hs zTransferFuture.__init__cCs|jS)N)r)r r r rr [szTransferFuture.metacCs |jS)N)rr)r r r rr_szTransferFuture.donec Cs>y |jStk r8}z||Wdd}~XYnXdS)N)rrKeyboardInterruptr)r er r rrbs  zTransferFuture.resultcCs|jdS)N)rr)r r r rrlszTransferFuture.cancelcCs$|std|jj|dddS)z!Sets the exception on the future.z?set_exception can only be called once the transfer is complete.T)overrideN)rrr set_exception)r exceptionr r rr#oszTransferFuture.set_exception)NN) rrrrrr rrrr#r r r rrGs    rc@sReZdZdZdddZeddZeddZed d Zed d Z d dZ dS)rz'Holds metadata about the TransferFutureNcCs||_||_d|_i|_dS)N) _call_args _transfer_id_size _user_context)r rrr r rr|szTransferMeta.__init__cCs|jS)z*The call args used in the transfer request)r%)r r r rrszTransferMeta.call_argscCs|jS)zThe unique id of the transfer)r&)r r r rrszTransferMeta.transfer_idcCs|jS)z)The size of the transfer request if known)r')r r r rsizeszTransferMeta.sizecCs|jS)z.A dictionary that requesters can store data in)r()r r r rrszTransferMeta.user_contextcCs ||_dS)zA method to provide the size of a transfer request By providing this value, the TransferManager will not try to call HeadObject or use the use OS to determine the size of the transfer. N)r')r r)r r rprovide_transfer_sizesz"TransferMeta.provide_transfer_size)NN) rrr__doc__rrrrr)rr*r r r rrys     rc@seZdZdZd5ddZddZeddZed d Zed d Z ed dZ ddZ d6ddZ ddZ defddZddZddZddZd7dd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdS)8rz*A helper class for managing TransferFutureNcCsb||_d|_d|_d|_t|_g|_g|_t |_ t |_ t |_ t |_t |_dS)Nz not-started)r_status_result _exceptionset_associated_futures_failure_cleanups_done_callbacks threadingEvent _done_eventLock_lock_associated_futures_lock_done_callbacks_lock_failure_cleanups_lock)r rr r rrs    zTransferCoordinator.__init__cCsd|jj|jS)Nz{}(transfer_id={}))format __class__rr)r r r r__repr__szTransferCoordinator.__repr__cCs|jS)N)r.)r r r rr$szTransferCoordinator.exceptionc Cs|jt|jSQRXdS)zThe list of futures associated to the inprogress TransferFuture Once the transfer finishes this list becomes empty as the transfer is considered done and there should be no running futures left. N)r8copyr0)r r r rassociated_futuressz&TransferCoordinator.associated_futurescCs|jS)z;The list of callbacks to call when the TransferFuture fails)r1)r r r rfailure_cleanupssz$TransferCoordinator.failure_cleanupscCs|jS)aThe status of the TransferFuture The currently supported states are: * not-started - Has yet to start. If in this state, a transfer can be canceled immediately and nothing will happen. * queued - SubmissionTask is about to submit tasks * running - Is inprogress. In-progress as of now means that the SubmissionTask that runs the transfer is being executed. So there is no guarantee any transfer requests had been made to S3 if this state is reached. * cancelled - Was cancelled * failed - An exception other than CancelledError was thrown * success - No exceptions were thrown and is done. )r,)r r r rstatusszTransferCoordinator.statusc Cs(|jd|_||_d|_WdQRXdS)aSet a result for the TransferFuture Implies that the TransferFuture succeeded. This will always set a result because it is invoked on the final task where there is only ever one final task and it is ran at the very end of a transfer process. So if a result is being set for this final task, the transfer succeeded even if something came a long and canceled the transfer on the final task. Nsuccess)r7r.r-r,)r rr r r set_results zTransferCoordinator.set_resultFc Cs.|j|r|r ||_d|_WdQRXdS)zSet an exception for the TransferFuture Implies the TransferFuture failed. :param exception: The exception that cause the transfer to fail. :param override: If True, override any existing state. failedN)r7rr.r,)r r$r"r r rr#s z!TransferCoordinator.set_exceptioncCs|jt|jr|j|jS)zWaits until TransferFuture is done and returns the result If the TransferFuture succeeded, it will return the result. If the TransferFuture failed, it will raise the exception associated to the failure. )r5waitrr.r-)r r r rrs zTransferCoordinator.resultc CsZ|jJ|sLd}td|||||_|jdkr:d}d|_|rL|WdQRXdS)zCancels the TransferFuture :param msg: The message to attach to the cancellation :param exc_type: The type of exception to set for the cancellation Fz%s cancel(%s) calledz not-startedT cancelledN)r7rloggerdebugr.r, announce_done)r msgexc_typeZshould_announce_doner r rr s  zTransferCoordinator.cancelcCs|ddS)z+Sets the TransferFutrue's status to runningZqueuedN)_transition_to_non_done_state)r r r rset_status_to_queuedsz(TransferCoordinator.set_status_to_queuedcCs|ddS)z+Sets the TransferFuture's status to runningrunningN)rM)r r r rset_status_to_running"sz)TransferCoordinator.set_status_to_runningc Cs6|j&|r"td|j|f||_WdQRXdS)Nz=Unable to transition from done state %s to non-done state %s.)r7r RuntimeErrorrAr,)r Z desired_stater r rrM&s z1TransferCoordinator._transition_to_non_done_statecCsDtd|||j|j||d}|||t|j||S)aSubmits a task to a provided executor :type executor: s3transfer.futures.BoundedExecutor :param executor: The executor to submit the callable to :type task: s3transfer.tasks.Task :param task: The task to submit to the executor :type tag: s3transfer.futures.TaskTag :param tag: A tag to associate to the submitted task :rtype: concurrent.futures.Future :returns: A future representing the submitted task z;Submitting task {} to executor {} for transfer request: {}.)tag) rHrIr;rsubmitadd_associated_futureadd_done_callbackrremove_associated_future)r executortaskrRfuturer r rrS/s zTransferCoordinator.submitcCs |jdkS)zDetermines if a TransferFuture has completed :returns: False if status is equal to 'failed', 'cancelled', or 'success'. True, otherwise )rDrGrB)rA)r r r rrLszTransferCoordinator.donec Cs"|j|j|WdQRXdS)z6Adds a future to be associated with the TransferFutureN)r8r0add)r rYr r rrTTsz)TransferCoordinator.add_associated_futurec Cs"|j|j|WdQRXdS)z4Removes a future's association to the TransferFutureN)r8r0remove)r rYr r rrVYsz,TransferCoordinator.remove_associated_futurec Os.|j|jt|f||WdQRXdS)z7Add a done callback to be invoked when transfer is doneN)r9r2appendr)r functionargskwargsr r rrU^sz%TransferCoordinator.add_done_callbackc Os.|j|jt|f||WdQRXdS)z$Adds a callback to call upon failureN)r:r1r\r)r r]r^r_r r radd_failure_cleanupesz'TransferCoordinator.add_failure_cleanupcCs(|jdkr||j|dS)aMAnnounce that future is done running and run associated callbacks This will run any failure cleanups if the transfer failed if not they have not been run, allows the result() to be unblocked, and will run any done callbacks associated to the TransferFuture if they have not already been ran. rBN)rA_run_failure_cleanupsr5r/_run_done_callbacks)r r r rrJls  z!TransferCoordinator.announce_donec Cs(|j||jg|_WdQRXdS)N)r9_run_callbacksr2)r r r rrbys z'TransferCoordinator._run_done_callbacksc Cs(|j||jg|_WdQRXdS)N)r:rcr@r1)r r r rras z)TransferCoordinator._run_failure_cleanupscCsx|D]}||qWdS)N) _run_callback)r callbackscallbackr r rrcs z"TransferCoordinator._run_callbackscCs6y |Wn&tk r0tjd|ddYnXdS)NzException raised in %s.T)exc_info) ExceptionrHrI)r rfr r rrds z!TransferCoordinator._run_callback)N)F)N)rrrr+rr=rr$r?r@rArCr#rrrrNrPrMrSrrTrVrUr`rJrbrarcrdr r r rrs2       rc@s0eZdZejZd ddZd ddZd ddZdS) BoundedExecutorNcCs6||_|dkr|j}||jd|_t||_||_dS)aFAn executor implementation that has a maximum queued up tasks The executor will block if the number of tasks that have been submitted and is currently working on is past its maximum. :params max_size: The maximum number of inflight futures. An inflight future means that the task is either queued up or is currently being executed. A size of None or 0 means that the executor will have no bound in terms of the number of inflight futures. :params max_num_threads: The maximum number of threads the executor uses. :type tag_semaphores: dict :params tag_semaphores: A dictionary where the key is the name of the tag and the value is the semaphore to use when limiting the number of tasks the executor is processing at a time. :type executor_cls: BaseExecutor :param underlying_executor_cls: The executor class that get bounded by this executor. If None is provided, the concurrent.futures.ThreadPoolExecutor class is used. N) max_workers)Z_max_num_threads EXECUTOR_CLS _executorr _semaphore_tag_semaphores)r max_sizeZmax_num_threadsZtag_semaphoresZ executor_clsr r rrs  zBoundedExecutor.__init__TcCsP|j}|r|j|}||j|}t|j|j|}t|j|}| ||S)a1Submit a task to complete :type task: s3transfer.tasks.Task :param task: The task to run __call__ on :type tag: s3transfer.futures.TaskTag :param tag: An optional tag to associate to the task. This is used to override which semaphore to use. :type block: boolean :param block: True if to wait till it is possible to submit a task. False, if not to wait and raise an error if not able to submit a task. :returns: The future associated to the submitted task ) rmrnacquirerrreleaseExecutorFuturerlrSrU)r rXrRblockZ semaphoreZ acquire_tokenZrelease_callbackrYr r rrSs  zBoundedExecutor.submitcCs|j|dS)N)rlshutdown)r rEr r rrtszBoundedExecutor.shutdown)NN)NT)T) rrrrThreadPoolExecutorrkrrSrtr r r rris &ric@s,eZdZddZddZddZddZd S) rrcCs ||_dS)aA future returned from the executor Currently, it is just a wrapper around a concurrent.futures.Future. However, this can eventually grow to implement the needed functionality of concurrent.futures.Future if we move off of the library and not affect the rest of the codebase. :type future: concurrent.futures.Future :param future: The underlying future N)_future)r rYr r rrs zExecutorFuture.__init__cCs |jS)N)rvr)r r r rrszExecutorFuture.resultcsfdd}|j|dS)aAdds a callback to be completed once future is done :param fn: A callable that takes no arguments. Note that is different than concurrent.futures.Future.add_done_callback that requires a single argument for the future. csS)Nr )Zfuture_passed_to_callback)fnr r done_callbacksz7ExecutorFuture.add_done_callback..done_callbackN)rvrU)r rwrxr )rwrrUs z ExecutorFuture.add_done_callbackcCs |jS)N)rvr)r r r rrszExecutorFuture.doneN)rrrrrrUrr r r rrrs rrc@s,eZdZdZd ddZddZd dd ZdS) BaseExecutorzABase Executor class implementation needed to work with s3transferNcCsdS)Nr )r rjr r rr szBaseExecutor.__init__cOs tddS)Nzsubmit())r )r rwr^r_r r rrSszBaseExecutor.submitTcCs tddS)Nz shutdown())r )r rEr r rrtszBaseExecutor.shutdown)N)T)rrrr+rrSrtr r r rrys ryc@s"eZdZdZddZdddZdS) NonThreadedExecutorz@A drop-in replacement non-threaded version of ThreadPoolExecutorc Osht}y|||}||WnDtk rbtdd\}}td||||||YnX|S)Nz0Setting exception for %s to %s with traceback %s)NonThreadedExecutorFuturerCrhsysrgrHrIset_exception_info)r rwr^r_rYrr!tbr r rrSs zNonThreadedExecutor.submitTcCsdS)Nr )r rEr r rrt(szNonThreadedExecutor.shutdownN)T)rrrr+rSrtr r r rrzsrzc@sReZdZdZddZddZddZdd d Zd d Zd dZ ddZ ddZ dS)r|zThe Future returned from NonThreadedExecutor Note that this future is **not** thread-safe as it is being used from the context of a non-threaded environment. cCs"d|_d|_d|_d|_g|_dS)NF)r-r. _traceback_doner2)r r r rr3s z"NonThreadedExecutorFuture.__init__cCs||_|dS)N)r- _set_done)r rr r rrC:sz$NonThreadedExecutorFuture.set_resultcCs||_||_|dS)N)r.rr)r r$ tracebackr r rr~>sz,NonThreadedExecutorFuture.set_exception_infoNcCs|jr|j|j|jS)N)r.with_tracebackrr-)r timeoutr r rrCsz NonThreadedExecutorFuture.resultcCs*d|_x|jD]}||qWg|_dS)NT)rr2_invoke_done_callback)r rxr r rrHs z#NonThreadedExecutorFuture._set_donecCs||S)Nr )r rxr r rrNsz/NonThreadedExecutorFuture._invoke_done_callbackcCs|jS)N)r)r r r rrQszNonThreadedExecutorFuture.donecCs"|jr||n |j|dS)N)rrr2r\)r rwr r rrUTs z+NonThreadedExecutorFuture.add_done_callback)N) rrrr+rrCr~rrrrrUr r r rr|,s r|TaskTagnameZin_memory_uploadZin_memory_download) r>loggingr}r3 collectionsr concurrentrZs3transfer.compatrZs3transfer.exceptionsrrZs3transfer.utilsrr getLoggerrrHr rrrrrirrryrzr|rZIN_MEMORY_UPLOAD_TAGZIN_MEMORY_DOWNLOAD_TAGr r r r s.    2'wN$ /