Save the session file less often

This commit is contained in:
Lonami Exo 2018-06-24 12:21:58 +02:00
parent 026c0c4f9d
commit d4479a0a4e
2 changed files with 9 additions and 4 deletions

View File

@ -215,6 +215,12 @@ class UpdateMethods(UserMethods):
# Just send them periodically. # Just send them periodically.
self._sender.send(functions.PingRequest(rnd())) self._sender.send(functions.PingRequest(rnd()))
# Entities and cached files are not saved when they are
# inserted because this is a rather expensive operation
# (default's sqlite3 takes ~0.1s to commit changes). Do
# it every minute instead. No-op if there's nothing new.
self.session.save()
# We need to send some content-related request at least hourly # We need to send some content-related request at least hourly
# for Telegram to keep delivering updates, otherwise they will # for Telegram to keep delivering updates, otherwise they will
# just stop even if we're connected. Do so every 30 minutes. # just stop even if we're connected. Do so every 30 minutes.

View File

@ -28,7 +28,6 @@ class SQLiteSession(MemorySession):
def __init__(self, session_id=None): def __init__(self, session_id=None):
super().__init__() super().__init__()
# These values will NOT be saved
self.filename = ':memory:' self.filename = ':memory:'
self.save_entities = True self.save_entities = True
@ -226,10 +225,11 @@ class SQLiteSession(MemorySession):
(entity_id, state.pts, state.qts, (entity_id, state.pts, state.qts,
state.date.timestamp(), state.seq)) state.date.timestamp(), state.seq))
c.close() c.close()
self.save()
def save(self): def save(self):
"""Saves the current session object as session_user_id.session""" """Saves the current session object as session_user_id.session"""
# This is a no-op if there are no changes to commit, so there's
# no need for us to keep track of an "unsaved changes" variable.
self._conn.commit() self._conn.commit()
def _cursor(self): def _cursor(self):
@ -243,6 +243,7 @@ class SQLiteSession(MemorySession):
"""Closes the connection unless we're working in-memory""" """Closes the connection unless we're working in-memory"""
if self.filename != ':memory:': if self.filename != ':memory:':
if self._conn is not None: if self._conn is not None:
self._conn.commit()
self._conn.close() self._conn.close()
self._conn = None self._conn = None
@ -282,7 +283,6 @@ class SQLiteSession(MemorySession):
self._cursor().executemany( self._cursor().executemany(
'insert or replace into entities values (?,?,?,?,?)', rows 'insert or replace into entities values (?,?,?,?,?)', rows
) )
self.save()
def _fetchone_entity(self, query, args): def _fetchone_entity(self, query, args):
c = self._cursor() c = self._cursor()
@ -337,4 +337,3 @@ class SQLiteSession(MemorySession):
_SentFileType.from_type(type(instance)).value, _SentFileType.from_type(type(instance)).value,
instance.id, instance.access_hash instance.id, instance.access_hash
)) ))
self.save()