asyncio \ queues.pyНеверно за исключением asyncio.Queue.put?
@coroutine
def put(self, item):
"""Put an item into the queue.
Put an item into the queue. If the queue is full, wait until a free
slot is available before adding item.
This method is a coroutine.
"""
while self.full():
putter = futures.Future(loop=self._loop)
self._putters.append(putter)
try:
yield from putter
except:
putter.cancel() # Just in case putter is not done yet.
if not self.full() and not putter.cancelled():
# We were woken up by get_nowait(), but can't take
# the call. Wake up the next in line.
self._wakeup_next(self._putters)
raise
return self.put_nowait(item)
На мой взгляд, putter
может быть сделано cancel
, set_exception
или set_result
. get_nowait
использование set_result
. только cancel
и set_exception
будут выбрасывать исключение, тогда может произойти except:
. Думаю, except:
не нужен.
Почему он добавляет except:
в Wake up the next in line
?
Update: @Vincent _wakeup_next
вызов set_result
. set_result
выполнит self._state = _FINISHED
. task1.cancel()
будет self._fut_waiter.cancel()
, которые возвращаются False. Итак, task1 будет не отменен.
@Vincent благодаря очень
основной причиной является task.cancel может отменить задачу, хотя в будущем, которое ждет задача была set_result (self._state = _FINISHED).
_wakeup_next call set_result. set_result выполнит 'self._state = _FINISHED'. task1.cancel() будет «self._fut_waiter.cancel()», который возвращает False. – Vince
@Vince 'task1' и' putter' - это не одно и то же будущее. Путч ('task1._fut_waiter') действительно закончен, но' task1' нет, пока он не будет отменен, а 'task1._must_cancel' будет установлен в' True'. – Vincent
В этом случае task1 ожидает (не запускается), когда task1.cancel, поэтому он просто отменяется перед запуском. get_nowait не пробуждает какой-либо клюшки (кроме задачи 1 не запускается). Возможно, нам нужно создать образец, чтобы отменить запущенную задачу1. – Vince