Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../asyncio
File: tasks.py
"""Support for tasks, coroutines and the scheduler."""
[0] Fix | Delete
[1] Fix | Delete
__all__ = (
[2] Fix | Delete
'Task', 'create_task',
[3] Fix | Delete
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
[4] Fix | Delete
'wait', 'wait_for', 'as_completed', 'sleep',
[5] Fix | Delete
'gather', 'shield', 'ensure_future', 'run_coroutine_threadsafe',
[6] Fix | Delete
'current_task', 'all_tasks',
[7] Fix | Delete
'_register_task', '_unregister_task', '_enter_task', '_leave_task',
[8] Fix | Delete
)
[9] Fix | Delete
[10] Fix | Delete
import concurrent.futures
[11] Fix | Delete
import contextvars
[12] Fix | Delete
import functools
[13] Fix | Delete
import inspect
[14] Fix | Delete
import itertools
[15] Fix | Delete
import types
[16] Fix | Delete
import warnings
[17] Fix | Delete
import weakref
[18] Fix | Delete
[19] Fix | Delete
from . import base_tasks
[20] Fix | Delete
from . import coroutines
[21] Fix | Delete
from . import events
[22] Fix | Delete
from . import exceptions
[23] Fix | Delete
from . import futures
[24] Fix | Delete
from .coroutines import _is_coroutine
[25] Fix | Delete
[26] Fix | Delete
# Helper to generate new task names
[27] Fix | Delete
# This uses itertools.count() instead of a "+= 1" operation because the latter
[28] Fix | Delete
# is not thread safe. See bpo-11866 for a longer explanation.
[29] Fix | Delete
_task_name_counter = itertools.count(1).__next__
[30] Fix | Delete
[31] Fix | Delete
[32] Fix | Delete
def current_task(loop=None):
[33] Fix | Delete
"""Return a currently executed task."""
[34] Fix | Delete
if loop is None:
[35] Fix | Delete
loop = events.get_running_loop()
[36] Fix | Delete
return _current_tasks.get(loop)
[37] Fix | Delete
[38] Fix | Delete
[39] Fix | Delete
def all_tasks(loop=None):
[40] Fix | Delete
"""Return a set of all tasks for the loop."""
[41] Fix | Delete
if loop is None:
[42] Fix | Delete
loop = events.get_running_loop()
[43] Fix | Delete
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
[44] Fix | Delete
# thread while we do so. Therefore we cast it to list prior to filtering. The list
[45] Fix | Delete
# cast itself requires iteration, so we repeat it several times ignoring
[46] Fix | Delete
# RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for
[47] Fix | Delete
# details.
[48] Fix | Delete
i = 0
[49] Fix | Delete
while True:
[50] Fix | Delete
try:
[51] Fix | Delete
tasks = list(_all_tasks)
[52] Fix | Delete
except RuntimeError:
[53] Fix | Delete
i += 1
[54] Fix | Delete
if i >= 1000:
[55] Fix | Delete
raise
[56] Fix | Delete
else:
[57] Fix | Delete
break
[58] Fix | Delete
return {t for t in tasks
[59] Fix | Delete
if futures._get_loop(t) is loop and not t.done()}
[60] Fix | Delete
[61] Fix | Delete
[62] Fix | Delete
def _all_tasks_compat(loop=None):
[63] Fix | Delete
# Different from "all_task()" by returning *all* Tasks, including
[64] Fix | Delete
# the completed ones. Used to implement deprecated "Tasks.all_task()"
[65] Fix | Delete
# method.
[66] Fix | Delete
if loop is None:
[67] Fix | Delete
loop = events.get_event_loop()
[68] Fix | Delete
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
[69] Fix | Delete
# thread while we do so. Therefore we cast it to list prior to filtering. The list
[70] Fix | Delete
# cast itself requires iteration, so we repeat it several times ignoring
[71] Fix | Delete
# RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for
[72] Fix | Delete
# details.
[73] Fix | Delete
i = 0
[74] Fix | Delete
while True:
[75] Fix | Delete
try:
[76] Fix | Delete
tasks = list(_all_tasks)
[77] Fix | Delete
except RuntimeError:
[78] Fix | Delete
i += 1
[79] Fix | Delete
if i >= 1000:
[80] Fix | Delete
raise
[81] Fix | Delete
else:
[82] Fix | Delete
break
[83] Fix | Delete
return {t for t in tasks if futures._get_loop(t) is loop}
[84] Fix | Delete
[85] Fix | Delete
[86] Fix | Delete
def _set_task_name(task, name):
[87] Fix | Delete
if name is not None:
[88] Fix | Delete
try:
[89] Fix | Delete
set_name = task.set_name
[90] Fix | Delete
except AttributeError:
[91] Fix | Delete
pass
[92] Fix | Delete
else:
[93] Fix | Delete
set_name(name)
[94] Fix | Delete
[95] Fix | Delete
[96] Fix | Delete
class Task(futures._PyFuture): # Inherit Python Task implementation
[97] Fix | Delete
# from a Python Future implementation.
[98] Fix | Delete
[99] Fix | Delete
"""A coroutine wrapped in a Future."""
[100] Fix | Delete
[101] Fix | Delete
# An important invariant maintained while a Task not done:
[102] Fix | Delete
#
[103] Fix | Delete
# - Either _fut_waiter is None, and _step() is scheduled;
[104] Fix | Delete
# - or _fut_waiter is some Future, and _step() is *not* scheduled.
[105] Fix | Delete
#
[106] Fix | Delete
# The only transition from the latter to the former is through
[107] Fix | Delete
# _wakeup(). When _fut_waiter is not None, one of its callbacks
[108] Fix | Delete
# must be _wakeup().
[109] Fix | Delete
[110] Fix | Delete
# If False, don't log a message if the task is destroyed whereas its
[111] Fix | Delete
# status is still pending
[112] Fix | Delete
_log_destroy_pending = True
[113] Fix | Delete
[114] Fix | Delete
@classmethod
[115] Fix | Delete
def current_task(cls, loop=None):
[116] Fix | Delete
"""Return the currently running task in an event loop or None.
[117] Fix | Delete
[118] Fix | Delete
By default the current task for the current event loop is returned.
[119] Fix | Delete
[120] Fix | Delete
None is returned when called not in the context of a Task.
[121] Fix | Delete
"""
[122] Fix | Delete
warnings.warn("Task.current_task() is deprecated since Python 3.7, "
[123] Fix | Delete
"use asyncio.current_task() instead",
[124] Fix | Delete
DeprecationWarning,
[125] Fix | Delete
stacklevel=2)
[126] Fix | Delete
if loop is None:
[127] Fix | Delete
loop = events.get_event_loop()
[128] Fix | Delete
return current_task(loop)
[129] Fix | Delete
[130] Fix | Delete
@classmethod
[131] Fix | Delete
def all_tasks(cls, loop=None):
[132] Fix | Delete
"""Return a set of all tasks for an event loop.
[133] Fix | Delete
[134] Fix | Delete
By default all tasks for the current event loop are returned.
[135] Fix | Delete
"""
[136] Fix | Delete
warnings.warn("Task.all_tasks() is deprecated since Python 3.7, "
[137] Fix | Delete
"use asyncio.all_tasks() instead",
[138] Fix | Delete
DeprecationWarning,
[139] Fix | Delete
stacklevel=2)
[140] Fix | Delete
return _all_tasks_compat(loop)
[141] Fix | Delete
[142] Fix | Delete
def __init__(self, coro, *, loop=None, name=None):
[143] Fix | Delete
super().__init__(loop=loop)
[144] Fix | Delete
if self._source_traceback:
[145] Fix | Delete
del self._source_traceback[-1]
[146] Fix | Delete
if not coroutines.iscoroutine(coro):
[147] Fix | Delete
# raise after Future.__init__(), attrs are required for __del__
[148] Fix | Delete
# prevent logging for pending task in __del__
[149] Fix | Delete
self._log_destroy_pending = False
[150] Fix | Delete
raise TypeError(f"a coroutine was expected, got {coro!r}")
[151] Fix | Delete
[152] Fix | Delete
if name is None:
[153] Fix | Delete
self._name = f'Task-{_task_name_counter()}'
[154] Fix | Delete
else:
[155] Fix | Delete
self._name = str(name)
[156] Fix | Delete
[157] Fix | Delete
self._must_cancel = False
[158] Fix | Delete
self._fut_waiter = None
[159] Fix | Delete
self._coro = coro
[160] Fix | Delete
self._context = contextvars.copy_context()
[161] Fix | Delete
[162] Fix | Delete
self._loop.call_soon(self.__step, context=self._context)
[163] Fix | Delete
_register_task(self)
[164] Fix | Delete
[165] Fix | Delete
def __del__(self):
[166] Fix | Delete
if self._state == futures._PENDING and self._log_destroy_pending:
[167] Fix | Delete
context = {
[168] Fix | Delete
'task': self,
[169] Fix | Delete
'message': 'Task was destroyed but it is pending!',
[170] Fix | Delete
}
[171] Fix | Delete
if self._source_traceback:
[172] Fix | Delete
context['source_traceback'] = self._source_traceback
[173] Fix | Delete
self._loop.call_exception_handler(context)
[174] Fix | Delete
super().__del__()
[175] Fix | Delete
[176] Fix | Delete
def _repr_info(self):
[177] Fix | Delete
return base_tasks._task_repr_info(self)
[178] Fix | Delete
[179] Fix | Delete
def get_coro(self):
[180] Fix | Delete
return self._coro
[181] Fix | Delete
[182] Fix | Delete
def get_name(self):
[183] Fix | Delete
return self._name
[184] Fix | Delete
[185] Fix | Delete
def set_name(self, value):
[186] Fix | Delete
self._name = str(value)
[187] Fix | Delete
[188] Fix | Delete
def set_result(self, result):
[189] Fix | Delete
raise RuntimeError('Task does not support set_result operation')
[190] Fix | Delete
[191] Fix | Delete
def set_exception(self, exception):
[192] Fix | Delete
raise RuntimeError('Task does not support set_exception operation')
[193] Fix | Delete
[194] Fix | Delete
def get_stack(self, *, limit=None):
[195] Fix | Delete
"""Return the list of stack frames for this task's coroutine.
[196] Fix | Delete
[197] Fix | Delete
If the coroutine is not done, this returns the stack where it is
[198] Fix | Delete
suspended. If the coroutine has completed successfully or was
[199] Fix | Delete
cancelled, this returns an empty list. If the coroutine was
[200] Fix | Delete
terminated by an exception, this returns the list of traceback
[201] Fix | Delete
frames.
[202] Fix | Delete
[203] Fix | Delete
The frames are always ordered from oldest to newest.
[204] Fix | Delete
[205] Fix | Delete
The optional limit gives the maximum number of frames to
[206] Fix | Delete
return; by default all available frames are returned. Its
[207] Fix | Delete
meaning differs depending on whether a stack or a traceback is
[208] Fix | Delete
returned: the newest frames of a stack are returned, but the
[209] Fix | Delete
oldest frames of a traceback are returned. (This matches the
[210] Fix | Delete
behavior of the traceback module.)
[211] Fix | Delete
[212] Fix | Delete
For reasons beyond our control, only one stack frame is
[213] Fix | Delete
returned for a suspended coroutine.
[214] Fix | Delete
"""
[215] Fix | Delete
return base_tasks._task_get_stack(self, limit)
[216] Fix | Delete
[217] Fix | Delete
def print_stack(self, *, limit=None, file=None):
[218] Fix | Delete
"""Print the stack or traceback for this task's coroutine.
[219] Fix | Delete
[220] Fix | Delete
This produces output similar to that of the traceback module,
[221] Fix | Delete
for the frames retrieved by get_stack(). The limit argument
[222] Fix | Delete
is passed to get_stack(). The file argument is an I/O stream
[223] Fix | Delete
to which the output is written; by default output is written
[224] Fix | Delete
to sys.stderr.
[225] Fix | Delete
"""
[226] Fix | Delete
return base_tasks._task_print_stack(self, limit, file)
[227] Fix | Delete
[228] Fix | Delete
def cancel(self):
[229] Fix | Delete
"""Request that this task cancel itself.
[230] Fix | Delete
[231] Fix | Delete
This arranges for a CancelledError to be thrown into the
[232] Fix | Delete
wrapped coroutine on the next cycle through the event loop.
[233] Fix | Delete
The coroutine then has a chance to clean up or even deny
[234] Fix | Delete
the request using try/except/finally.
[235] Fix | Delete
[236] Fix | Delete
Unlike Future.cancel, this does not guarantee that the
[237] Fix | Delete
task will be cancelled: the exception might be caught and
[238] Fix | Delete
acted upon, delaying cancellation of the task or preventing
[239] Fix | Delete
cancellation completely. The task may also return a value or
[240] Fix | Delete
raise a different exception.
[241] Fix | Delete
[242] Fix | Delete
Immediately after this method is called, Task.cancelled() will
[243] Fix | Delete
not return True (unless the task was already cancelled). A
[244] Fix | Delete
task will be marked as cancelled when the wrapped coroutine
[245] Fix | Delete
terminates with a CancelledError exception (even if cancel()
[246] Fix | Delete
was not called).
[247] Fix | Delete
"""
[248] Fix | Delete
self._log_traceback = False
[249] Fix | Delete
if self.done():
[250] Fix | Delete
return False
[251] Fix | Delete
if self._fut_waiter is not None:
[252] Fix | Delete
if self._fut_waiter.cancel():
[253] Fix | Delete
# Leave self._fut_waiter; it may be a Task that
[254] Fix | Delete
# catches and ignores the cancellation so we may have
[255] Fix | Delete
# to cancel it again later.
[256] Fix | Delete
return True
[257] Fix | Delete
# It must be the case that self.__step is already scheduled.
[258] Fix | Delete
self._must_cancel = True
[259] Fix | Delete
return True
[260] Fix | Delete
[261] Fix | Delete
def __step(self, exc=None):
[262] Fix | Delete
if self.done():
[263] Fix | Delete
raise exceptions.InvalidStateError(
[264] Fix | Delete
f'_step(): already done: {self!r}, {exc!r}')
[265] Fix | Delete
if self._must_cancel:
[266] Fix | Delete
if not isinstance(exc, exceptions.CancelledError):
[267] Fix | Delete
exc = exceptions.CancelledError()
[268] Fix | Delete
self._must_cancel = False
[269] Fix | Delete
coro = self._coro
[270] Fix | Delete
self._fut_waiter = None
[271] Fix | Delete
[272] Fix | Delete
_enter_task(self._loop, self)
[273] Fix | Delete
# Call either coro.throw(exc) or coro.send(None).
[274] Fix | Delete
try:
[275] Fix | Delete
if exc is None:
[276] Fix | Delete
# We use the `send` method directly, because coroutines
[277] Fix | Delete
# don't have `__iter__` and `__next__` methods.
[278] Fix | Delete
result = coro.send(None)
[279] Fix | Delete
else:
[280] Fix | Delete
result = coro.throw(exc)
[281] Fix | Delete
except StopIteration as exc:
[282] Fix | Delete
if self._must_cancel:
[283] Fix | Delete
# Task is cancelled right before coro stops.
[284] Fix | Delete
self._must_cancel = False
[285] Fix | Delete
super().cancel()
[286] Fix | Delete
else:
[287] Fix | Delete
super().set_result(exc.value)
[288] Fix | Delete
except exceptions.CancelledError:
[289] Fix | Delete
super().cancel() # I.e., Future.cancel(self).
[290] Fix | Delete
except (KeyboardInterrupt, SystemExit) as exc:
[291] Fix | Delete
super().set_exception(exc)
[292] Fix | Delete
raise
[293] Fix | Delete
except BaseException as exc:
[294] Fix | Delete
super().set_exception(exc)
[295] Fix | Delete
else:
[296] Fix | Delete
blocking = getattr(result, '_asyncio_future_blocking', None)
[297] Fix | Delete
if blocking is not None:
[298] Fix | Delete
# Yielded Future must come from Future.__iter__().
[299] Fix | Delete
if futures._get_loop(result) is not self._loop:
[300] Fix | Delete
new_exc = RuntimeError(
[301] Fix | Delete
f'Task {self!r} got Future '
[302] Fix | Delete
f'{result!r} attached to a different loop')
[303] Fix | Delete
self._loop.call_soon(
[304] Fix | Delete
self.__step, new_exc, context=self._context)
[305] Fix | Delete
elif blocking:
[306] Fix | Delete
if result is self:
[307] Fix | Delete
new_exc = RuntimeError(
[308] Fix | Delete
f'Task cannot await on itself: {self!r}')
[309] Fix | Delete
self._loop.call_soon(
[310] Fix | Delete
self.__step, new_exc, context=self._context)
[311] Fix | Delete
else:
[312] Fix | Delete
result._asyncio_future_blocking = False
[313] Fix | Delete
result.add_done_callback(
[314] Fix | Delete
self.__wakeup, context=self._context)
[315] Fix | Delete
self._fut_waiter = result
[316] Fix | Delete
if self._must_cancel:
[317] Fix | Delete
if self._fut_waiter.cancel():
[318] Fix | Delete
self._must_cancel = False
[319] Fix | Delete
else:
[320] Fix | Delete
new_exc = RuntimeError(
[321] Fix | Delete
f'yield was used instead of yield from '
[322] Fix | Delete
f'in task {self!r} with {result!r}')
[323] Fix | Delete
self._loop.call_soon(
[324] Fix | Delete
self.__step, new_exc, context=self._context)
[325] Fix | Delete
[326] Fix | Delete
elif result is None:
[327] Fix | Delete
# Bare yield relinquishes control for one event loop iteration.
[328] Fix | Delete
self._loop.call_soon(self.__step, context=self._context)
[329] Fix | Delete
elif inspect.isgenerator(result):
[330] Fix | Delete
# Yielding a generator is just wrong.
[331] Fix | Delete
new_exc = RuntimeError(
[332] Fix | Delete
f'yield was used instead of yield from for '
[333] Fix | Delete
f'generator in task {self!r} with {result!r}')
[334] Fix | Delete
self._loop.call_soon(
[335] Fix | Delete
self.__step, new_exc, context=self._context)
[336] Fix | Delete
else:
[337] Fix | Delete
# Yielding something else is an error.
[338] Fix | Delete
new_exc = RuntimeError(f'Task got bad yield: {result!r}')
[339] Fix | Delete
self._loop.call_soon(
[340] Fix | Delete
self.__step, new_exc, context=self._context)
[341] Fix | Delete
finally:
[342] Fix | Delete
_leave_task(self._loop, self)
[343] Fix | Delete
self = None # Needed to break cycles when an exception occurs.
[344] Fix | Delete
[345] Fix | Delete
def __wakeup(self, future):
[346] Fix | Delete
try:
[347] Fix | Delete
future.result()
[348] Fix | Delete
except BaseException as exc:
[349] Fix | Delete
# This may also be a cancellation.
[350] Fix | Delete
self.__step(exc)
[351] Fix | Delete
else:
[352] Fix | Delete
# Don't pass the value of `future.result()` explicitly,
[353] Fix | Delete
# as `Future.__iter__` and `Future.__await__` don't need it.
[354] Fix | Delete
# If we call `_step(value, None)` instead of `_step()`,
[355] Fix | Delete
# Python eval loop would use `.send(value)` method call,
[356] Fix | Delete
# instead of `__next__()`, which is slower for futures
[357] Fix | Delete
# that return non-generator iterators from their `__iter__`.
[358] Fix | Delete
self.__step()
[359] Fix | Delete
self = None # Needed to break cycles when an exception occurs.
[360] Fix | Delete
[361] Fix | Delete
[362] Fix | Delete
_PyTask = Task
[363] Fix | Delete
[364] Fix | Delete
[365] Fix | Delete
try:
[366] Fix | Delete
import _asyncio
[367] Fix | Delete
except ImportError:
[368] Fix | Delete
pass
[369] Fix | Delete
else:
[370] Fix | Delete
# _CTask is needed for tests.
[371] Fix | Delete
Task = _CTask = _asyncio.Task
[372] Fix | Delete
[373] Fix | Delete
[374] Fix | Delete
def create_task(coro, *, name=None):
[375] Fix | Delete
"""Schedule the execution of a coroutine object in a spawn task.
[376] Fix | Delete
[377] Fix | Delete
Return a Task object.
[378] Fix | Delete
"""
[379] Fix | Delete
loop = events.get_running_loop()
[380] Fix | Delete
task = loop.create_task(coro)
[381] Fix | Delete
_set_task_name(task, name)
[382] Fix | Delete
return task
[383] Fix | Delete
[384] Fix | Delete
[385] Fix | Delete
# wait() and as_completed() similar to those in PEP 3148.
[386] Fix | Delete
[387] Fix | Delete
FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED
[388] Fix | Delete
FIRST_EXCEPTION = concurrent.futures.FIRST_EXCEPTION
[389] Fix | Delete
ALL_COMPLETED = concurrent.futures.ALL_COMPLETED
[390] Fix | Delete
[391] Fix | Delete
[392] Fix | Delete
async def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
[393] Fix | Delete
"""Wait for the Futures and coroutines given by fs to complete.
[394] Fix | Delete
[395] Fix | Delete
The fs iterable must not be empty.
[396] Fix | Delete
[397] Fix | Delete
Coroutines will be wrapped in Tasks.
[398] Fix | Delete
[399] Fix | Delete
Returns two sets of Future: (done, pending).
[400] Fix | Delete
[401] Fix | Delete
Usage:
[402] Fix | Delete
[403] Fix | Delete
done, pending = await asyncio.wait(fs)
[404] Fix | Delete
[405] Fix | Delete
Note: This does not raise TimeoutError! Futures that aren't done
[406] Fix | Delete
when the timeout occurs are returned in the second set.
[407] Fix | Delete
"""
[408] Fix | Delete
if futures.isfuture(fs) or coroutines.iscoroutine(fs):
[409] Fix | Delete
raise TypeError(f"expect a list of futures, not {type(fs).__name__}")
[410] Fix | Delete
if not fs:
[411] Fix | Delete
raise ValueError('Set of coroutines/Futures is empty.')
[412] Fix | Delete
if return_when not in (FIRST_COMPLETED, FIRST_EXCEPTION, ALL_COMPLETED):
[413] Fix | Delete
raise ValueError(f'Invalid return_when value: {return_when}')
[414] Fix | Delete
[415] Fix | Delete
if loop is None:
[416] Fix | Delete
loop = events.get_running_loop()
[417] Fix | Delete
else:
[418] Fix | Delete
warnings.warn("The loop argument is deprecated since Python 3.8, "
[419] Fix | Delete
"and scheduled for removal in Python 3.10.",
[420] Fix | Delete
DeprecationWarning, stacklevel=2)
[421] Fix | Delete
[422] Fix | Delete
fs = {ensure_future(f, loop=loop) for f in set(fs)}
[423] Fix | Delete
[424] Fix | Delete
return await _wait(fs, timeout, return_when, loop)
[425] Fix | Delete
[426] Fix | Delete
[427] Fix | Delete
def _release_waiter(waiter, *args):
[428] Fix | Delete
if not waiter.done():
[429] Fix | Delete
waiter.set_result(None)
[430] Fix | Delete
[431] Fix | Delete
[432] Fix | Delete
async def wait_for(fut, timeout, *, loop=None):
[433] Fix | Delete
"""Wait for the single Future or coroutine to complete, with timeout.
[434] Fix | Delete
[435] Fix | Delete
Coroutine will be wrapped in Task.
[436] Fix | Delete
[437] Fix | Delete
Returns result of the Future or coroutine. When a timeout occurs,
[438] Fix | Delete
it cancels the task and raises TimeoutError. To avoid the task
[439] Fix | Delete
cancellation, wrap it in shield().
[440] Fix | Delete
[441] Fix | Delete
If the wait is cancelled, the task is also cancelled.
[442] Fix | Delete
[443] Fix | Delete
This function is a coroutine.
[444] Fix | Delete
"""
[445] Fix | Delete
if loop is None:
[446] Fix | Delete
loop = events.get_running_loop()
[447] Fix | Delete
else:
[448] Fix | Delete
warnings.warn("The loop argument is deprecated since Python 3.8, "
[449] Fix | Delete
"and scheduled for removal in Python 3.10.",
[450] Fix | Delete
DeprecationWarning, stacklevel=2)
[451] Fix | Delete
[452] Fix | Delete
if timeout is None:
[453] Fix | Delete
return await fut
[454] Fix | Delete
[455] Fix | Delete
if timeout <= 0:
[456] Fix | Delete
fut = ensure_future(fut, loop=loop)
[457] Fix | Delete
[458] Fix | Delete
if fut.done():
[459] Fix | Delete
return fut.result()
[460] Fix | Delete
[461] Fix | Delete
await _cancel_and_wait(fut, loop=loop)
[462] Fix | Delete
try:
[463] Fix | Delete
fut.result()
[464] Fix | Delete
except exceptions.CancelledError as exc:
[465] Fix | Delete
raise exceptions.TimeoutError() from exc
[466] Fix | Delete
else:
[467] Fix | Delete
raise exceptions.TimeoutError()
[468] Fix | Delete
[469] Fix | Delete
waiter = loop.create_future()
[470] Fix | Delete
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
[471] Fix | Delete
cb = functools.partial(_release_waiter, waiter)
[472] Fix | Delete
[473] Fix | Delete
fut = ensure_future(fut, loop=loop)
[474] Fix | Delete
fut.add_done_callback(cb)
[475] Fix | Delete
[476] Fix | Delete
try:
[477] Fix | Delete
# wait until the future completes or the timeout
[478] Fix | Delete
try:
[479] Fix | Delete
await waiter
[480] Fix | Delete
except exceptions.CancelledError:
[481] Fix | Delete
if fut.done():
[482] Fix | Delete
return fut.result()
[483] Fix | Delete
else:
[484] Fix | Delete
fut.remove_done_callback(cb)
[485] Fix | Delete
# We must ensure that the task is not running
[486] Fix | Delete
# after wait_for() returns.
[487] Fix | Delete
# See https://bugs.python.org/issue32751
[488] Fix | Delete
await _cancel_and_wait(fut, loop=loop)
[489] Fix | Delete
raise
[490] Fix | Delete
[491] Fix | Delete
if fut.done():
[492] Fix | Delete
return fut.result()
[493] Fix | Delete
else:
[494] Fix | Delete
fut.remove_done_callback(cb)
[495] Fix | Delete
# We must ensure that the task is not running
[496] Fix | Delete
# after wait_for() returns.
[497] Fix | Delete
# See https://bugs.python.org/issue32751
[498] Fix | Delete
await _cancel_and_wait(fut, loop=loop)
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function