3 \a @sdddddddddd d d d g Zd dlZd dlZd dlZd dlZd dlZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddl m Z Gddde jZeZy d dlZWnek rYn XejZZej jZej jZej jZe ddedddZddZe ddddZe ddZdddd dZe d.ddd!dZddd"d#Zeed<de_[ddd$d Z e d%d&Z!Gd'd(d(e jZ"dd)d*d+d Z#ddd,d Z$d-d Z%dS)/TaskFIRST_COMPLETEDFIRST_EXCEPTION ALL_COMPLETEDwaitwait_for as_completedsleepasyncgathershield ensure_futurerun_coroutine_threadsafeN) base_tasks)compat) coroutines)events)futures) coroutinecseZdZejZiZdZedddZ edddZ ddfdd Z e j rTd d Zd d ZddddZdddddZddZdfdd ZddZZS)rTNcCs|dkrtj}|jj|S)N)rget_event_loop_current_tasksget)clsloopr%/usr/lib64/python3.6/asyncio/tasks.py current_task.szTask.current_taskcs$dkrtjfdd|jDS)Ncsh|]}|jkr|qSr)_loop).0t)rrr Bsz!Task.all_tasks..)rr _all_tasks)rrr)rr all_tasks:szTask.all_tasks)rcsNtj|d|jr|jd=||_d|_d|_|jj|j|j j j |dS)N)rrF) super__init___source_traceback_coro _fut_waiter _must_cancelr call_soon_step __class__r"add)selfcoror)r-rrr&Dsz Task.__init__cCsH|jtjkr8|jr8|dd}|jr,|j|d<|jj|tjj|dS)Nz%Task was destroyed but it is pending!)taskmessageZsource_traceback) Z_staterZ_PENDING_log_destroy_pendingr'rZcall_exception_handlerFuture__del__)r/contextrrrr5Ss  z Task.__del__cCs tj|S)N)rZ_task_repr_info)r/rrr _repr_info^szTask._repr_info)limitcCs tj||S)N)rZ_task_get_stack)r/r8rrr get_stackaszTask.get_stack)r8filecCstj|||S)N)rZ_task_print_stack)r/r8r:rrr print_stackxs zTask.print_stackcCs4d|_|jrdS|jdk r*|jjr*dSd|_dS)NFT)Z_log_tracebackdoner)cancelr*)r/rrrr=s  z Task.cancelcsf|jr t|tjstj}d|_|j}d|_||jj|j<zy"|dkrT|j d}n |j |}Wnt k r}z.|jrd|_|j tjn |j |jWYdd}~Xntjk rtjYn|tk r}z|j |WYdd}~XnPtk r(}z|j |WYdd}~Xn Xt|dd}|dk r|j|jk rl|jj|jtdj||n||r||kr|jj|jtdj|n2d|_|j|j||_|jr|jjrd|_n|jj|jtdj||n^|dkr|jj|jnDtj|r.|jj|jtdj||n|jj|jtdj|Wd|jjj|jd}XdS)NF_asyncio_future_blockingz6Task {!r} got Future {!r} attached to a different loopz!Task cannot await on itself: {!r}z;yield was used instead of yield from in task {!r} with {!r}zIyield was used instead of yield from for generator in task {!r} with {!r}zTask got bad yield: {!r})r* isinstancerCancelledErrorr(r)r-rrsendthrow StopIteration set_exception set_resultvaluer%r= Exception BaseExceptiongetattrr+r, RuntimeErrorformatr>add_done_callback_wakeupinspectZ isgeneratorpop)r/excr0resultZblocking)r-rrr,s~           z Task._stepcCsJy |jWn,tk r8}z|j|WYdd}~Xn X|jd}dS)N)rQrGr,)r/futurerPrrrrMs  z Task._wakeup)N)N)N)__name__ __module__ __qualname__weakrefWeakSetr"rr3 classmethodrr#r&rZPY34r5r7r9r;r=r,rM __classcell__rr)r-rrs      !T)rtimeout return_whenc#stj|stj|r&tdt|j|s2td|tt t fkrNtdj |dkr^t j fddt|D}t|||EdHS)Nz expect a list of futures, not %sz#Set of coroutines/Futures is empty.zInvalid return_when value: {}csh|]}t|dqS))r)r )rf)rrrr!7szwait..)risfuturer iscoroutine TypeErrortyperS ValueErrorrrrrKrrset_wait)fsrrZr[r)rrrscGs|js|jddS)N)r<rE)waiterargsrrr_release_waiter<srg)rccs|dkrtj}|dkr"|EdHS|j}|j|t|}tjt|}t||d}|j|zhy|EdHWn*t j k r|j ||j YnX|j r|jS|j ||j t jWd|j XdS)N)r)rr create_future call_laterrg functoolspartialr rLrr@remove_done_callbackr=r<rQ TimeoutError)futrZrretimeout_handlecbrrrrAs,       c #s|jd|dk r"|j|tt|fdd}x|D]}|j|qBWzEdHWddk rtjXtt}}x4|D],}|j||jr|j |q|j |qW||fS)Ncs\d8dks6tks6tkrX|j rX|jdk rXdk rFjjsXjddS)Nrr)rr cancelled exceptionr=r<rE)r\)counterr[rorerr_on_completion|s z_wait.._on_completion) rhrirglenrLr=rbrlr<r.)rdrZr[rrtr\r<pendingr)rsr[rorerrcos&     rc)rrZc#stj|stj|r&tdt|jdk r2ntjfddt |Dddl m }|ddfdd}fd d t fd d }xD]}|j qWr|dk rʈj||xttD] }|VqWdS) Nz expect a list of futures, not %scsh|]}t|dqS))r)r )rr\)rrrr!szas_completed..r)Queue)rcs.x D]}|jjdqWjdS)N)rl put_nowaitclear)r\)rtr<todorr _on_timeouts  z!as_completed.._on_timeoutcs6sdSj|j| r2dk r2jdS)N)removerxr=)r\)r<rorzrrrts   z$as_completed.._on_completionc3s$jEdH}|dkrtj|jS)N)rrrmrQ)r\)r<rr _wait_for_onesz#as_completed.._wait_for_one)rr]rr^r_r`rSrrrbZqueuesrwrrLrirangeru)rdrrZrwr{r}r\_r)rtr<rrorzrrs      c csX|dkrdV|S|dkr"tj}|j}|jj|tj||}z |EdHS|jXdS)Nr)rrrhrrirZ_set_result_unless_cancelledr=)ZdelayrQrrRhrrrrs cCstjdtddt||dS)Nz;asyncio.async() function is deprecated, use ensure_future()) stacklevel)r)warningswarnDeprecationWarningr )coro_or_futurerrrrasync_srcCstj|r(|dk r$||jk r$td|Stj|r^|dkrBtj}|j|}|j rZ|j d=|St j r~t j |r~tt||dStddS)Nz$loop argument must agree with Futurer)rz:An asyncio.Future, a coroutine or an awaitable is requiredr$)rr]rrarr^rrZ create_taskr'rZPY35rNZ isawaitabler _wrap_awaitabler_)rrr1rrrr s   ccs|jEdHS)N) __await__)Z awaitablerrrrsrcs*eZdZddfdd ZddZZS)_GatheringFutureN)rcstj|d||_d|_dS)N)rF)r%r& _children_cancel_requested)r/childrenr)r-rrr&$sz_GatheringFuture.__init__cCs:|jr dSd}x|jD]}|jrd}qW|r6d|_|S)NFT)r<rr=r)r/ZretZchildrrrr=)s z_GatheringFuture.cancel)rSrTrUr&r=rYrr)r-rrsrF)rreturn_exceptionscs|s*|dkrtj}|jjgSixjt|D]^}tj|sht||d}|dkr`|j}d|_ n&|}|dkr||j}n|j|k rt d||<q8Wfdd|D}t |t ||dddgfdd}x&t |D]\}}|jtj||qWS) N)rFz)futures are tied to different event loopscsg|] }|qSrr)rarg) arg_to_futrr hszgather..rcsjr|js|jdS|jr@tj}slj|dSn,|jdk rf|j}slj|dSn|j}||<d7krjrjtjn j dS)Nr) r<rqrrrr@rDZ _exceptionZ_resultrrE)irnres) nchildren nfinishedouterresultsrrr_done_callbackns*   zgather.._done_callback)rrrhrErbrr]r rr3rarur enumeraterLrjrk)rrZcoros_or_futuresrrnrrrr)rrrrrrrr 8s8       cs@t||d}|jr|S|j}|jfdd}|j|S)N)rcs\jr|js|jdS|jr.jn*|j}|dk rJj|nj|jdS)N)rqrrr=rDrErQ)innerrP)rrrrs  zshield.._done_callback)r r<rrhrL)rrrrr)rrr s   cs:tjstdtjjfdd}j|S)NzA coroutine object is requiredcsTytjtdWn6tk rN}zjr<j|WYdd}~XnXdS)N)r)rZ _chain_futurer rGZset_running_or_notify_cancelrD)rP)r0rRrrrcallbacks  z*run_coroutine_threadsafe..callback)rr^r_ concurrentrr4Zcall_soon_threadsafe)r0rrr)r0rRrrr s    )N)&__all__Zconcurrent.futuresrrjrNrrVrrrrrrr4rZ_PyTaskZ_asyncio ImportErrorZ_CTaskrrrrrgrrcrrrglobalsrSr rrr r r rrrrsX        s  - -8  W5