From f29b8bb7e4703fe40b90742124c8224616f4fbb3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 1 May 2018 21:08:47 +0200 Subject: [PATCH] Cache the session right after they start. Related to #68 @EriHoss is right, the session can know how to clean itself in case for some reason it fails to start, hence it makes much more sense to cache sessions right after they start. Also, clear the media_sessions dict when stopping to get rid of closed sessions. --- pyrogram/client/client.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index ae8bf87f..676f861b 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -423,6 +423,8 @@ class Client: for i in self.media_sessions.values(): i.stop() + self.media_sessions.clear() + self.is_started = False self.session.stop() @@ -2992,10 +2994,10 @@ class Client: self.api_id ) - self.media_sessions[dc_id] = session - session.start() + self.media_sessions[dc_id] = session + session.send( functions.auth.ImportAuthorization( id=exported_auth.id, @@ -3011,10 +3013,10 @@ class Client: self.api_id ) - self.media_sessions[dc_id] = session - session.start() + self.media_sessions[dc_id] = session + if volume_id: # Photos are accessed by volume_id, local_id, secret location = types.InputFileLocation( volume_id=volume_id, @@ -3082,10 +3084,10 @@ class Client: is_cdn=True ) - self.media_sessions[r.dc_id] = cdn_session - cdn_session.start() + self.media_sessions[r.dc_id] = cdn_session + try: with tempfile.NamedTemporaryFile("wb", delete=False) as f: file_name = f.name