Your IP : 18.222.197.93
B
� f�w�@s@dZdZddlZddlZddlZddlZddlZddlZddl Z ddl
mZddl
mZddl
m
Z
ddl
mZdd lmZd>d
d�Zd?dd
�Zd@dd�ZGdd�dej�ZeZyddlZWnek
r�YnXejZZdd�ZejjZejjZejjZdded�dd�Zdd�Zdd�dd�Zdd�Z dd�Z!ddd �d!d"�Z"ejd#d$��Z#dAdd�d%d&�Z$dd�d'd(�Z%ed)d*��Z&Gd+d,�d,ej'�Z(dd-d.�d/d0�Z)dd�d1d2�Z*d3d4�Z+e �,�Z-iZ.d5d6�Z/d7d8�Z0d9d:�Z1d;d<�Z2e/Z3e2Z4e0Z5e1Z6y$dd=lm/Z/m2Z2m0Z0m1Z1m-Z-m.Z.Wnek
�r*YnXe/Z7e2Z8e0Z9e1Z:dS)Bz0Support for tasks, coroutines and the scheduler.)�Task�create_task�FIRST_COMPLETED�FIRST_EXCEPTION�
ALL_COMPLETED�wait�wait_for�as_completed�sleep�gather�shield�
ensure_future�run_coroutine_threadsafe�current_task� all_tasks�_register_task�_unregister_task�_enter_task�_leave_task�N�)�
base_tasks)�
coroutines)�events)�futures)� coroutinecCs|dkrt��}t�|�S)z!Return a currently executed task.N)r�get_running_loop�_current_tasks�get)�loop�r�2/opt/alt/python37/lib64/python3.7/asyncio/tasks.pyrsrcsb�dkrt���d}x:ytt�}Wn&tk
rH|d7}|dkrD�YqXPqW�fdd�|D�S)z'Return a set of all tasks for the loop.Nrri�cs&h|]}t�|��kr|��s|�qSr)r� _get_loop�done)�.0�t)rrr � <setcomp>5szall_tasks.<locals>.<setcomp>)rr�list�
_all_tasks�RuntimeError)r�i�tasksr)rr r"srcsb�dkrt���d}x:ytt�}Wn&tk
rH|d7}|dkrD�YqXPqW�fdd�|D�S)Nrri�csh|]}t�|��kr|�qSr)rr!)r#r$)rrr r%Nsz$_all_tasks_compat.<locals>.<setcomp>)r�get_event_loopr&r'r()rr)r*r)rr �_all_tasks_compat9sr,cs�eZdZdZdZeddd��Zed dd��Zdd��fd d
�
Z�fdd�Z d
d�Z
dd�Zdd�Zdd�dd�Z
ddd�dd�Zdd�Zd!�fdd� Zdd�Z�ZS)"rz A coroutine wrapped in a Future.TNcCs(tjdtdd�|dkr t��}t|�S)z�Return the currently running task in an event loop or None.
By default the current task for the current event loop is returned.
None is returned when called not in the context of a Task.
zETask.current_task() is deprecated, use asyncio.current_task() instead�)�
stacklevelN)�warnings�warn�PendingDeprecationWarningrr+r)�clsrrrr rcszTask.current_taskcCstjdtdd�t|�S)z|Return a set of all tasks for an event loop.
By default all tasks for the current event loop are returned.
z?Task.all_tasks() is deprecated, use asyncio.all_tasks() insteadr-)r.)r/r0r1r,)r2rrrr rsszTask.all_tasks)rcsvt�j|d�|jr|jd=t�|�s:d|_td|����d|_d|_||_ t
��|_|j
j|j|jd�t|�dS)N)r���Fza coroutine was expected, got )�context)�super�__init__�_source_tracebackr�iscoroutine�_log_destroy_pending� TypeError�_must_cancel�_fut_waiter�_coro�contextvarsZcopy_context�_context�_loop� call_soon�_Task__stepr)�self�coror)� __class__rr r6s
z
Task.__init__csF|jtjkr8|jr8|dd�}|jr,|j|d<|j�|�t���dS)Nz%Task was destroyed but it is pending!)�task�messageZsource_traceback) Z_staterZ_PENDINGr9r7r@Zcall_exception_handlerr5�__del__)rCr4)rErr rH�s
zTask.__del__cCs
t�|�S)N)rZ_task_repr_info)rCrrr �
_repr_info�szTask._repr_infocCstd��dS)Nz*Task does not support set_result operation)r()rC�resultrrr �
set_result�szTask.set_resultcCstd��dS)Nz-Task does not support set_exception operation)r()rC� exceptionrrr �
set_exception�szTask.set_exception)�limitcCst�||�S)a�Return the list of stack frames for this task's coroutine.
If the coroutine is not done, this returns the stack where it is
suspended. If the coroutine has completed successfully or was
cancelled, this returns an empty list. If the coroutine was
terminated by an exception, this returns the list of traceback
frames.
The frames are always ordered from oldest to newest.
The optional limit gives the maximum number of frames to
return; by default all available frames are returned. Its
meaning differs depending on whether a stack or a traceback is
returned: the newest frames of a stack are returned, but the
oldest frames of a traceback are returned. (This matches the
behavior of the traceback module.)
For reasons beyond our control, only one stack frame is
returned for a suspended coroutine.
)rZ_task_get_stack)rCrNrrr � get_stack�szTask.get_stack)rN�filecCst�|||�S)anPrint the stack or traceback for this task's coroutine.
This produces output similar to that of the traceback module,
for the frames retrieved by get_stack(). The limit argument
is passed to get_stack(). The file argument is an I/O stream
to which the output is written; by default output is written
to sys.stderr.
)rZ_task_print_stack)rCrNrPrrr �print_stack�s zTask.print_stackcCs4d|_|��rdS|jdk r*|j��r*dSd|_dS)a�Request that this task cancel itself.
This arranges for a CancelledError to be thrown into the
wrapped coroutine on the next cycle through the event loop.
The coroutine then has a chance to clean up or even deny
the request using try/except/finally.
Unlike Future.cancel, this does not guarantee that the
task will be cancelled: the exception might be caught and
acted upon, delaying cancellation of the task or preventing
cancellation completely. The task may also return a value or
raise a different exception.
Immediately after this method is called, Task.cancelled() will
not return True (unless the task was already cancelled). A
task will be marked as cancelled when the wrapped coroutine
terminates with a CancelledError exception (even if cancel()
was not called).
FNT)Z_log_tracebackr"r<�cancelr;)rCrrr rR�s
zTask.cancelc
s�|��rt�d|�d|����|jr>t|tj�s8t��}d|_|j}d|_t|j |��zhy"|dkrp|�
d�}n
|�|�}Wn�tk
r�}z0|jr�d|_t
��t���nt
��|j�Wdd}~XY�n�tjk
r�t
���Y�n�tk
�r}zt
��|�Wdd}~XY�n�tk
�rN}zt
��|��Wdd}~XY�npXt|dd�}|dk �rBt�|�|j k �r�td|�d|�d��}|j j|j||jd�n�|�r||k�r�td |���}|j j|j||jd�n8d|_|j|j|jd�||_|j�r@|j���r@d|_n*td
|�d|���}|j j|j||jd�n||dk�rb|j j|j|jd�n\t�|��r�td|�d|���}|j j|j||jd�n$td
|���}|j j|j||jd�Wdt|j |�d}XdS)Nz_step(): already done: z, F�_asyncio_future_blockingzTask z got Future z attached to a different loop)r4zTask cannot await on itself: z-yield was used instead of yield from in task z with z;yield was used instead of yield from for generator in task zTask got bad yield: ) r"rZInvalidStateErrorr;�
isinstance�CancelledErrorr=r<rr@�send�throw�
StopIterationr5rMrK�valuerR� Exception�
BaseException�getattrr!r(rArBr?rS�add_done_callback�
_Task__wakeup�inspectZisgeneratorr)rC�excrDrJZblocking�new_exc)rErr Z__step�s|
zTask.__stepc
CsJy|��Wn,tk
r8}z|�|�Wdd}~XYn
X|��d}dS)N)rJrZrB)rC�futurer`rrr Z__wakeup<sz
Task.__wakeup)N)N)N)�__name__�
__module__�__qualname__�__doc__r9�classmethodrrr6rHrIrKrMrOrQrRrBr^�
__classcell__rr)rEr rQs
!TrcCst��}|�|�S)z]Schedule the execution of a coroutine object in a spawn task.
Return a Task object.
)rrr)rDrrrr rYsr)r�timeout�return_whenc�s�t�|�st�|�r(tdt|�j����|s4td��|tt t
fkrPtd|�����dkr`t����fdd�t
|�D�}t|||��IdHS)a�Wait for the Futures and coroutines given by fs to complete.
The sequence futures must not be empty.
Coroutines will be wrapped in Tasks.
Returns two sets of Future: (done, pending).
Usage:
done, pending = await asyncio.wait(fs)
Note: This does not raise TimeoutError! Futures that aren't done
when the timeout occurs are returned in the second set.
zexpect a list of futures, not z#Set of coroutines/Futures is empty.zInvalid return_when value: Ncsh|]}t|�d��qS))r)r)r#�f)rrr r%�szwait.<locals>.<setcomp>)r�isfuturerr8r:�typerc�
ValueErrorrrrrr+�set�_wait)�fsrrirjr)rr risrcGs|��s|�d�dS)N)r"rK)�waiter�argsrrr �_release_waiter�srt)rc�s|dkrt��}|dkr"|IdHS|dkrVt||d�}|��rF|��S|��t���|��}|� |t
|�}t�t
|�}t||d�}|�
|�zry|IdHWn*tjk
r�|�|�|���YnX|��r�|��S|�|�t||d�IdHt���Wd|��XdS)a�Wait for the single Future or coroutine to complete, with timeout.
Coroutine will be wrapped in Task.
Returns result of the Future or coroutine. When a timeout occurs,
it cancels the task and raises TimeoutError. To avoid the task
cancellation, wrap it in shield().
If the wait is cancelled, the task is also cancelled.
This function is a coroutine.
Nr)r)rr+rr"rJrRr�TimeoutError�
create_future�
call_laterrt� functools�partialr]rU�remove_done_callback�_cancel_and_wait)�futrirrr�timeout_handle�cbrrr r�s8
rc
�s�|���d�|dk r"|�|t���t|������fdd�}x|D]}|�|�qBWz�IdHWd�dk rt���x|D]}|�|�qzWXt�t�}}x*|D]"}|��r�|� |�q�|� |�q�W||fS)zVInternal helper for wait().
The fs argument must be a collection of Futures.
NcsZ�d8��dks4�tks4�tkrV|��sV|��dk rV�dk rD������sV��d�dS)Nrr)rr� cancelledrLrRr"rK)rk)�counterrjr}rrrr �_on_completion�sz_wait.<locals>._on_completion)
rvrwrt�lenr]rRrzror"�add)rqrirjrr�rkr"Zpendingr)r�rjr}rrr rp�s(
rpc �sF|��}t�t|�}|�|�z|��|IdHWd|�|�XdS)z<Cancel the *fut* future or task and wait until it completes.N)rvrxryrtr]rRrz)r|rrrr~rrr r{�s
r{)rric#s�t�|�st�|�r(tdt|�j�����dk r4�nt����fdd�t |�D��ddl
m}|�d��d����fdd �}���fd
d���fdd
�}x�D]}|���q�W�r�|dk rȈ�
||��xtt���D]}|�Vq�WdS)a^Return an iterator whose values are coroutines.
When waiting for the yielded coroutines you'll get the results (or
exceptions!) of the original Futures (or coroutines), in the order
in which and as soon as they complete.
This differs from PEP 3148; the proper way to use this is:
for f in as_completed(fs):
result = await f # The 'await' may raise.
# Use result.
If a timeout is specified, the 'await' will raise
TimeoutError when the timeout occurs before all Futures are done.
Note: The futures 'f' are not necessarily members of fs.
zexpect a list of futures, not Ncsh|]}t|�d��qS))r)r)r#rk)rrr r%szas_completed.<locals>.<setcomp>r)�Queue)rcs.x �D]}|�����d�qW���dS)N)rz�
put_nowait�clear)rk)r�r"�todorr �_on_timeouts
z!as_completed.<locals>._on_timeoutcs4�sdS��|���|��s0�dk r0���dS)N)�remover�rR)rk)r"r}r�rr r�#s
z$as_completed.<locals>._on_completionc�s$���IdH}|dkrtj�|��S)N)rrrurJ)rk)r"rr �
_wait_for_one+sz#as_completed.<locals>._wait_for_one)rrlrr8r:rmrcrr+roZqueuesr�r]rw�ranger�)rqrrir�r�r�rk�_r)r�r"rr}r�r rs
rccs
dVdS)z�Skip one event loop run cycle.
This is a private helper for 'asyncio.sleep()', used
when the 'delay' is set to 0. It uses a bare 'yield'
expression (which Task.__step knows how to handle)
instead of creating a Future object.
Nrrrrr �__sleep0:s r�c�s\|dkrt�IdH|S|dkr(t��}|��}|�|tj||�}z
|IdHS|��XdS)z9Coroutine that completes after a given time (in seconds).rN)r�rr+rvrwrZ_set_result_unless_cancelledrR)ZdelayrJrrb�hrrr r Fs
r cCs�t�|�r6|dkrt��}|�|�}|jr2|jd=|St�|�rb|dk r^|t�|�k r^t d��|St
�|�r|tt
|�|d�Std��dS)zmWrap a coroutine or an awaitable in a future.
If the argument is a Future, it is returned directly.
Nr3z$loop argument must agree with Future)rz:An asyncio.Future, a coroutine or an awaitable is required)rr8rr+rr7rrlr!rnr_Zisawaitabler�_wrap_awaitabler:)Zcoro_or_futurerrFrrr rXs
rccs|��EdHS)z�Helper for asyncio.ensure_future().
Wraps awaitable (an object with __await__) into a coroutine
that will later be wrapped in a Task by ensure_future().
N)� __await__)Z awaitablerrr r�osr�cs.eZdZdZdd��fdd�
Zdd�Z�ZS)�_GatheringFuturez�Helper for gather().
This overrides cancel() to cancel all the children and act more
like Task.cancel(), which doesn't immediately mark itself as
cancelled.
N)rcst�j|d�||_d|_dS)N)rF)r5r6� _children�_cancel_requested)rC�childrenr)rErr r6�sz_GatheringFuture.__init__cCs:|��rdSd}x|jD]}|��rd}qW|r6d|_|S)NFT)r"r�rRr�)rCZretZchildrrr rR�sz_GatheringFuture.cancel)rcrdrerfr6rRrhrr)rEr r�ysr�F)r�return_exceptionscs�|s*|dkrt��}|�����g��S�����fdd�}i}g�d�d�xn|D]f}||kr�t||d�}|dkr~t�|�}||k r�d|_�d7�|||<|�|�n||}�� |�qTWt
�|d���S)a0Return a future aggregating results from the given coroutines/futures.
Coroutines will be wrapped in a future and scheduled in the event
loop. They will not necessarily be scheduled in the same order as
passed in.
All futures must share the same event loop. If all the tasks are
done successfully, the returned future's result is the list of
results (in the order of the original sequence, not necessarily
the order of results arrival). If *return_exceptions* is True,
exceptions in the tasks are treated the same as successful
results, and gathered in the result list; otherwise, the first
raised exception will be immediately propagated to the returned
future.
Cancellation: if the outer Future is cancelled, all children (that
have not completed yet) are also cancelled. If any child is
cancelled, this is treated as if it raised CancelledError --
the outer Future is *not* cancelled in this case. (This is to
prevent the cancellation of one child to cause other children to
be cancelled.)
Ncs��d7����r$|��s |��dS�sd|��rFt��}��|�dS|��}|dk rd��|�dS��kr�g}x@�D]8}|��r�t��}n|��}|dkr�|��}|�|�qvW�jrȈ�t���n
�� |�dS)Nr)
r"rrLrrUrMrJ�appendr�rK)r|r`ZresultsZres)r�� nfinished�nfuts�outerr�rr �_done_callback�s4
zgather.<locals>._done_callbackr)rFr)rr+rvrKrrr!r9r]r�r�)rr�Zcoros_or_futuresr�Z
arg_to_fut�argr|r)r�r�r�r�r�r r
�s2
1
r
cs\t||d�����r�St���}|����fdd����fdd�}������|��S)a.Wait for a future, shielding it from cancellation.
The statement
res = await shield(something())
is exactly equivalent to the statement
res = await something()
*except* that if the coroutine containing it is cancelled, the
task running in something() is not cancelled. From the POV of
something(), the cancellation did not happen. But its caller is
still cancelled, so the yield-from expression still raises
CancelledError. Note: If something() is cancelled by other means
this will still cancel shield().
If you want to completely ignore cancellation (not recommended)
you can combine shield() with a try/except clause, as follows:
try:
res = await shield(something())
except CancelledError:
res = None
)rcs\���r|��s|��dS|��r.���n*|��}|dk rJ��|�n��|���dS)N)rrLrRrMrKrJ)�innerr`)r�rr �_inner_done_callback#s
z$shield.<locals>._inner_done_callbackcs���s����dS)N)r"rz)r�)r�r�rr �_outer_done_callback4sz$shield.<locals>._outer_done_callback)rr"rr!rvr])r�rr�r)r�r�r�r rs
rcs:t���std��tj������fdd�}��|��S)zsSubmit a coroutine object to a given event loop.
Return a concurrent.futures.Future to access the result.
zA coroutine object is requiredc
sTyt�t��d���Wn6tk
rN}z���r<��|��Wdd}~XYnXdS)N)r)rZ
_chain_futurerrZZset_running_or_notify_cancelrM)r`)rDrbrrr �callbackFs
z*run_coroutine_threadsafe.<locals>.callback)rr8r:�
concurrentr�FutureZcall_soon_threadsafe)rDrr�r)rDrbrr r
=s
r
cCst�|�dS)z3Register a new task in asyncio as executed by loop.N)r'r�)rFrrr rZsrcCs4t�|�}|dk r(td|�d|�d���|t|<dS)NzCannot enter into task z while another task z is being executed.)rrr()rrFrrrr r_s
rcCs2t�|�}||k r(td|�d|�d���t|=dS)Nz
Leaving task z! does not match the current task �.)rrr()rrFrrrr rgs
rcCst�|�dS)zUnregister a task.N)r'�discard)rFrrr rosr)rrrrr'r)N)N)N)N);rf�__all__Zconcurrent.futuresr�r>rxr_�typesr/�weakref�rrrrrrrr,Z _PyFuturerZ_PyTaskZ_asyncio�ImportErrorZ_CTaskrrrrrrtrrpr{rr�r rr�r�r�r
rr
ZWeakSetr'rrrrrZ_py_register_taskZ_py_unregister_taskZ_py_enter_taskZ_py_leave_taskZ_c_register_taskZ_c_unregister_taskZ
_c_enter_taskZ
_c_leave_taskrrrr �<module>sx
}
9,7
m;$
?>