Added file locking to avoid db contention

This commit is contained in:
tamservo
2025-05-03 22:07:31 -04:00
parent 4b636eed7a
commit 839ee6306c
2 changed files with 51 additions and 44 deletions

View File

@@ -7,6 +7,8 @@ import time
import sqlite3
import sys
from contextlib import closing
from filelock import Timeout, FileLock
from fastf1.signalr_aio import Connection
import fastf1
@@ -85,7 +87,8 @@ class SignalRClient:
"SessionData", "LapCount"]
self.debug = debug
self._db_is_open = False
self.messages_db = 'messages.db'
self.messages_lock = FileLock('messages.lock')
self.filename = filename
self.filemode = filemode
self.timeout = timeout
@@ -97,6 +100,7 @@ class SignalRClient:
stream=sys.stderr
)
self.logger = logging.getLogger('SignalR')
self.logger.warning("Created logger for SignalR")
else:
self.logger = logger
@@ -104,23 +108,15 @@ class SignalRClient:
self._t_last_message = None
def _to_file(self, msg):
"""
self._output_file.write(msg + '\n')
self._output_file.flush()
"""
#print(msg)
con = sqlite3.connect('messages.db')
try:
with con:
self._db_is_open = True
con.execute("insert into messages (message) values(?)", (msg,))
con.close()
self.db_is_open = False
with self.messages_lock:
with closing(sqlite3.connect(self.messages_db)) as con:
with closing(con.cursor()) as cur:
#self.logger.warning("about to log: " + msg)
cur.execute("insert into messages (message) values(?)", (msg,))
con.commit()
except:
if con is not None:
con.close()
self._db_is_open = False
print(f'Error writing message to db.')
async def _on_do_nothing(self, msg):
# just do nothing with the message; intended for debug mode where some
@@ -196,8 +192,6 @@ class SignalRClient:
await asyncio.gather(asyncio.ensure_future(self._supervise()),
asyncio.ensure_future(self._run()))
#self._output_file.close()
while(self._db_is_open):
asyncio.sleep(1)
self.logger.warning("Exiting...")
def start(self):