mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-04 19:12:29 +00:00
Revert disconnect() to be async again (#1133)
It's the only way to properly clean all background tasks, which the library makes heavy use for in MTProto/Connection send and receive loops. Some parts of the code even relied on the fact that it was asynchronous (it used to return a future so you could await it and not be breaking). It's automatically syncified to reduce the damage of being a breaking change.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""Various helpers not related to the Telegram API itself"""
|
||||
import asyncio
|
||||
import os
|
||||
import struct
|
||||
from hashlib import sha1, sha256
|
||||
@@ -85,6 +86,22 @@ def retry_range(retries):
|
||||
yield 1 + attempt
|
||||
|
||||
|
||||
async def _cancel(log, **tasks):
|
||||
"""
|
||||
Helper to cancel one or more tasks gracefully, logging exceptions.
|
||||
"""
|
||||
for name, task in tasks.items():
|
||||
if not task:
|
||||
continue
|
||||
|
||||
task.cancel()
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except Exception:
|
||||
log.exception('Unhandled exception from %s after cancel', name)
|
||||
|
||||
# endregion
|
||||
|
||||
# region Cryptographic related utils
|
||||
|
Reference in New Issue
Block a user