2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 21:38:04 +00:00

Performance improvements

This commit is contained in:
Dan 2019-06-01 13:18:48 +02:00
parent f6361cd20f
commit d243ebc2cd
3 changed files with 22 additions and 31 deletions

View File

@ -765,7 +765,9 @@ class Client(Methods, BaseClient):
types.Channel, types.ChannelForbidden types.Channel, types.ChannelForbidden
] ]
] ]
): ) -> bool:
is_min = False
for entity in entities: for entity in entities:
if isinstance(entity, types.User): if isinstance(entity, types.User):
user_id = entity.id user_id = entity.id
@ -773,6 +775,7 @@ class Client(Methods, BaseClient):
access_hash = entity.access_hash access_hash = entity.access_hash
if access_hash is None: if access_hash is None:
is_min = True
continue continue
username = entity.username username = entity.username
@ -808,6 +811,7 @@ class Client(Methods, BaseClient):
access_hash = entity.access_hash access_hash = entity.access_hash
if access_hash is None: if access_hash is None:
is_min = True
continue continue
username = getattr(entity, "username", None) username = getattr(entity, "username", None)
@ -822,6 +826,8 @@ class Client(Methods, BaseClient):
if username is not None: if username is not None:
self.peers_by_username[username.lower()] = input_peer self.peers_by_username[username.lower()] = input_peer
return is_min
def download_worker(self): def download_worker(self):
name = threading.current_thread().name name = threading.current_thread().name
log.debug("{} started".format(name)) log.debug("{} started".format(name))
@ -837,7 +843,6 @@ class Client(Methods, BaseClient):
try: try:
data, file_name, done, progress, progress_args, path = packet data, file_name, done, progress, progress_args, path = packet
data = data # type: BaseClient.FileData
directory, file_name = os.path.split(file_name) directory, file_name = os.path.split(file_name)
directory = directory or "downloads" directory = directory or "downloads"
@ -917,8 +922,10 @@ class Client(Methods, BaseClient):
try: try:
if isinstance(updates, (types.Update, types.UpdatesCombined)): if isinstance(updates, (types.Update, types.UpdatesCombined)):
self.fetch_peers(updates.users) is_min = self.fetch_peers(updates.users) or self.fetch_peers(updates.chats)
self.fetch_peers(updates.chats)
users = {u.id: u for u in updates.users}
chats = {c.id: c for c in updates.chats}
for update in updates.updates: for update in updates.updates:
channel_id = getattr( channel_id = getattr(
@ -935,7 +942,7 @@ class Client(Methods, BaseClient):
if isinstance(update, types.UpdateChannelTooLong): if isinstance(update, types.UpdateChannelTooLong):
log.warning(update) log.warning(update)
if isinstance(update, types.UpdateNewChannelMessage): if isinstance(update, types.UpdateNewChannelMessage) and is_min:
message = update.message message = update.message
if not isinstance(message, types.MessageEmpty): if not isinstance(message, types.MessageEmpty):
@ -957,22 +964,10 @@ class Client(Methods, BaseClient):
pass pass
else: else:
if not isinstance(diff, types.updates.ChannelDifferenceEmpty): if not isinstance(diff, types.updates.ChannelDifferenceEmpty):
updates.users += diff.users users.update({u.id: u for u in diff.users})
updates.chats += diff.chats chats.update({c.id: c for c in diff.chats})
if channel_id and pts: self.dispatcher.updates_queue.put((update, users, chats))
if channel_id not in self.channels_pts:
self.channels_pts[channel_id] = []
if pts in self.channels_pts[channel_id]:
continue
self.channels_pts[channel_id].append(pts)
if len(self.channels_pts[channel_id]) > 50:
self.channels_pts[channel_id] = self.channels_pts[channel_id][25:]
self.dispatcher.updates_queue.put((update, updates.users, updates.chats))
elif isinstance(updates, (types.UpdateShortMessage, types.UpdateShortChatMessage)): elif isinstance(updates, (types.UpdateShortMessage, types.UpdateShortChatMessage)):
diff = self.send( diff = self.send(
functions.updates.GetDifference( functions.updates.GetDifference(
@ -989,13 +984,13 @@ class Client(Methods, BaseClient):
pts=updates.pts, pts=updates.pts,
pts_count=updates.pts_count pts_count=updates.pts_count
), ),
diff.users, {u.id: u for u in diff.users},
diff.chats {c.id: c for c in diff.chats}
)) ))
else: else:
self.dispatcher.updates_queue.put((diff.other_updates[0], [], [])) self.dispatcher.updates_queue.put((diff.other_updates[0], {}, {}))
elif isinstance(updates, types.UpdateShort): elif isinstance(updates, types.UpdateShort):
self.dispatcher.updates_queue.put((updates.update, [], [])) self.dispatcher.updates_queue.put((updates.update, {}, {}))
elif isinstance(updates, types.UpdatesTooLong): elif isinstance(updates, types.UpdatesTooLong):
log.warning(updates) log.warning(updates)
except Exception as e: except Exception as e:

View File

@ -96,7 +96,6 @@ class BaseClient:
self.date = None self.date = None
self.rnd_id = MsgId self.rnd_id = MsgId
self.channels_pts = {}
self.peers_by_id = {} self.peers_by_id = {}
self.peers_by_username = {} self.peers_by_username = {}

View File

@ -125,16 +125,13 @@ class Dispatcher:
log.debug("{} started".format(name)) log.debug("{} started".format(name))
while True: while True:
update = self.updates_queue.get() packet = self.updates_queue.get()
if update is None: if packet is None:
break break
try: try:
users = {i.id: i for i in update[1]} update, users, chats = packet
chats = {i.id: i for i in update[2]}
update = update[0]
parser = self.update_parsers.get(type(update), None) parser = self.update_parsers.get(type(update), None)
parsed_update, handler_type = ( parsed_update, handler_type = (