Support custom event loops

This commit is contained in:
Lonami Exo
2018-06-14 19:35:12 +02:00
parent 908dfa148b
commit 0f14f3b16a
11 changed files with 48 additions and 40 deletions
+2 -1
View File
@@ -217,7 +217,8 @@ class MessageMethods(UploadMethods, MessageParseMethods):
else:
request.max_date = r.messages[-1].date
await asyncio.sleep(max(wait_time - (time.time() - start), 0))
await asyncio.sleep(
max(wait_time - (time.time() - start), 0), loop=self._loop)
async def get_messages(self, *args, **kwargs):
"""
+16 -9
View File
@@ -1,4 +1,5 @@
import abc
import asyncio
import logging
import platform
import warnings
@@ -112,7 +113,8 @@ class TelegramBaseClient(abc.ABC):
system_version=None,
app_version=None,
lang_code='en',
system_lang_code='en'):
system_lang_code='en',
loop=None):
"""Refer to TelegramClient.__init__ for docs on this method"""
if not api_id or not api_hash:
raise ValueError(
@@ -120,6 +122,7 @@ class TelegramBaseClient(abc.ABC):
"Refer to telethon.rtfd.io for more information.")
self._use_ipv6 = use_ipv6
self._loop = loop or asyncio.get_event_loop()
# Determine what session object we have
if isinstance(session, str) or session is None:
@@ -143,12 +146,9 @@ class TelegramBaseClient(abc.ABC):
self.api_id = int(api_id)
self.api_hash = api_hash
# This is the main sender, which will be used from the thread
# that calls .connect(). Every other thread will spawn a new
# temporary connection. The connection on this one is always
# kept open so Telegram can send us updates.
if isinstance(connection, type):
connection = connection(proxy=proxy, timeout=timeout)
connection = connection(
proxy=proxy, timeout=timeout, loop=self._loop)
# Used on connection. Capture the variables in a lambda since
# exporting clients need to create this InvokeWithLayerRequest.
@@ -169,7 +169,7 @@ class TelegramBaseClient(abc.ABC):
state = MTProtoState(self.session.auth_key)
self._connection = connection
self._sender = MTProtoSender(
state, connection,
state, connection, self._loop,
first_query=self._init_with(functions.help.GetConfigRequest()),
update_callback=self._handle_update
)
@@ -211,6 +211,14 @@ class TelegramBaseClient(abc.ABC):
# endregion
# region Properties
@property
def loop(self):
return self._loop
# endregion
# region Connecting
async def connect(self):
@@ -287,12 +295,11 @@ class TelegramBaseClient(abc.ABC):
auth = self._exported_auths.get(dc_id)
dc = await self._get_dc(dc_id)
state = MTProtoState(auth)
# TODO Don't hardcode ConnectionTcpFull()
# Can't reuse self._sender._connection as it has its own seqno.
#
# If one were to do that, Telegram would reset the connection
# with no further clues.
sender = MTProtoSender(state, ConnectionTcpFull())
sender = MTProtoSender(state, self._connection.clone(), self._loop)
await sender.connect(dc.ip_address, dc.port)
if not auth:
__log__.info('Exporting authorization for data center %s', dc)
+1 -1
View File
@@ -144,7 +144,7 @@ class UpdateMethods(UserMethods):
# region Private methods
def _handle_update(self, update):
asyncio.ensure_future(self._dispatch_update(update))
asyncio.ensure_future(self._dispatch_update(update), loop=self._loop)
async def _dispatch_update(self, update):
if self._events_pending_resolve:
+1 -1
View File
@@ -30,7 +30,7 @@ class UserMethods(TelegramBaseClient):
pass
except (errors.FloodWaitError, errors.FloodTestPhoneWaitError) as e:
if e.seconds <= self.session.flood_sleep_threshold:
await asyncio.sleep(e.seconds)
await asyncio.sleep(e.seconds, loop=self._loop)
else:
raise
except (errors.PhoneMigrateError, errors.NetworkMigrateError,