mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-22 13:06:37 +00:00
Replace wait code with asyncio.wait_for and pass the client's loop
This commit is contained in:
parent
8ce001318e
commit
d3a9dcd36c
@ -182,18 +182,7 @@ class Conversation(ChatGetter):
|
|||||||
# Otherwise the next incoming response will be the one to use
|
# Otherwise the next incoming response will be the one to use
|
||||||
future = asyncio.Future()
|
future = asyncio.Future()
|
||||||
pending[target_id] = future
|
pending[target_id] = future
|
||||||
done, pending = await asyncio.wait(
|
return await self._get_result(future, now, timeout)
|
||||||
[future, self._sleep(now, timeout)],
|
|
||||||
return_when=asyncio.FIRST_COMPLETED
|
|
||||||
)
|
|
||||||
if future in pending:
|
|
||||||
for future in pending:
|
|
||||||
future.cancel()
|
|
||||||
|
|
||||||
raise asyncio.TimeoutError()
|
|
||||||
else:
|
|
||||||
return future.result()
|
|
||||||
|
|
||||||
|
|
||||||
async def get_edit(self, message=None, *, timeout=None):
|
async def get_edit(self, message=None, *, timeout=None):
|
||||||
"""
|
"""
|
||||||
@ -218,17 +207,7 @@ class Conversation(ChatGetter):
|
|||||||
# Otherwise the next incoming response will be the one to use
|
# Otherwise the next incoming response will be the one to use
|
||||||
future = asyncio.Future()
|
future = asyncio.Future()
|
||||||
self._pending_edits[target_id] = future
|
self._pending_edits[target_id] = future
|
||||||
done, pending = await asyncio.wait(
|
return await self._get_result(future, now, timeout)
|
||||||
[future, self._sleep(now, timeout)],
|
|
||||||
return_when=asyncio.FIRST_COMPLETED
|
|
||||||
)
|
|
||||||
if future in pending:
|
|
||||||
for future in pending:
|
|
||||||
future.cancel()
|
|
||||||
|
|
||||||
raise asyncio.TimeoutError()
|
|
||||||
else:
|
|
||||||
return future.result()
|
|
||||||
|
|
||||||
async def wait_read(self, message=None, *, timeout=None):
|
async def wait_read(self, message=None, *, timeout=None):
|
||||||
"""
|
"""
|
||||||
@ -247,17 +226,7 @@ class Conversation(ChatGetter):
|
|||||||
return
|
return
|
||||||
|
|
||||||
self._pending_reads[target_id] = future
|
self._pending_reads[target_id] = future
|
||||||
done, pending = await asyncio.wait(
|
return await self._get_result(future, now, timeout)
|
||||||
[future, self._sleep(now, timeout)],
|
|
||||||
return_when=asyncio.FIRST_COMPLETED
|
|
||||||
)
|
|
||||||
if future in pending:
|
|
||||||
for future in pending:
|
|
||||||
future.cancel()
|
|
||||||
|
|
||||||
raise asyncio.TimeoutError()
|
|
||||||
else:
|
|
||||||
return future.result()
|
|
||||||
|
|
||||||
def wait_event(self, event, *, timeout=None):
|
def wait_event(self, event, *, timeout=None):
|
||||||
"""
|
"""
|
||||||
@ -292,18 +261,10 @@ class Conversation(ChatGetter):
|
|||||||
|
|
||||||
future = asyncio.Future()
|
future = asyncio.Future()
|
||||||
async def result():
|
async def result():
|
||||||
done, pending = await asyncio.wait(
|
try:
|
||||||
[future, self._sleep(now, timeout)],
|
return await self._get_result(future, now, timeout)
|
||||||
return_when=asyncio.FIRST_COMPLETED
|
finally:
|
||||||
)
|
del self._custom[counter]
|
||||||
del self._custom[counter]
|
|
||||||
if future in pending:
|
|
||||||
for x in pending:
|
|
||||||
x.cancel()
|
|
||||||
|
|
||||||
raise asyncio.TimeoutError()
|
|
||||||
else:
|
|
||||||
return future.result()
|
|
||||||
|
|
||||||
self._custom[counter] = (event, future, False)
|
self._custom[counter] = (event, future, False)
|
||||||
return result()
|
return result()
|
||||||
@ -398,22 +359,19 @@ class Conversation(ChatGetter):
|
|||||||
else:
|
else:
|
||||||
raise ValueError('No message was sent previously')
|
raise ValueError('No message was sent previously')
|
||||||
|
|
||||||
async def _sleep(self, now, timeout):
|
async def _get_result(self, future, start_time, timeout):
|
||||||
due = self._total_due
|
due = self._total_due
|
||||||
if timeout is None:
|
if timeout is None:
|
||||||
timeout = self._timeout
|
timeout = self._timeout
|
||||||
|
|
||||||
if timeout is not None:
|
if timeout is not None:
|
||||||
due = min(due, now + timeout)
|
due = min(due, start_time + timeout)
|
||||||
|
|
||||||
try:
|
return await asyncio.wait_for(
|
||||||
if due == float('inf'):
|
future,
|
||||||
while True:
|
timeout=None if due == float('inf') else due - time.time(),
|
||||||
await asyncio.sleep(60)
|
loop=self._client.loop
|
||||||
elif due > now:
|
)
|
||||||
await asyncio.sleep(due - now)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def _cancel_all(self, exception=None):
|
def _cancel_all(self, exception=None):
|
||||||
for pending in itertools.chain(
|
for pending in itertools.chain(
|
||||||
|
Loading…
Reference in New Issue
Block a user