mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 13:29:47 +00:00
Abstract the send queue off MTProtoSender
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""Various helpers not related to the Telegram API itself"""
|
||||
import asyncio
|
||||
import collections
|
||||
import os
|
||||
import struct
|
||||
@@ -87,4 +88,42 @@ class TotalList(list):
|
||||
return '[{}, total={}]'.format(
|
||||
', '.join(repr(x) for x in self), self.total)
|
||||
|
||||
|
||||
class _ReadyQueue:
|
||||
"""
|
||||
A queue list that supports an arbitrary cancellation token for `get`.
|
||||
"""
|
||||
def __init__(self, loop):
|
||||
self._list = []
|
||||
self._loop = loop
|
||||
self._ready = asyncio.Event(loop=loop)
|
||||
|
||||
def append(self, item):
|
||||
self._list.append(item)
|
||||
self._ready.set()
|
||||
|
||||
def extend(self, items):
|
||||
self._list.extend(items)
|
||||
self._ready.set()
|
||||
|
||||
async def get(self, cancellation):
|
||||
"""
|
||||
Returns a list of all the items added to the queue until now and
|
||||
clears the list from the queue itself. Returns ``None`` if cancelled.
|
||||
"""
|
||||
ready = asyncio.ensure_future(self._ready.wait(), loop=self._loop)
|
||||
done, pending = await asyncio.wait(
|
||||
[ready, cancellation],
|
||||
return_when=asyncio.FIRST_COMPLETED,
|
||||
loop=self._loop
|
||||
)
|
||||
if cancellation in done:
|
||||
ready.cancel()
|
||||
return None
|
||||
|
||||
result = self._list
|
||||
self._list = []
|
||||
self._ready.clear()
|
||||
return result
|
||||
|
||||
# endregion
|
||||
|
Reference in New Issue
Block a user