optimize channel_db:
- use python objects mirrored by sql database - write sql to file asynchronously - the sql decorator is awaited in sweepstore, not in channel_db
This commit is contained in:
@@ -2,6 +2,7 @@ import os
|
||||
import concurrent
|
||||
import queue
|
||||
import threading
|
||||
import asyncio
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.pool import StaticPool
|
||||
@@ -18,28 +19,32 @@ def sql(func):
|
||||
"""wrapper for sql methods"""
|
||||
def wrapper(self, *args, **kwargs):
|
||||
assert threading.currentThread() != self.sql_thread
|
||||
f = concurrent.futures.Future()
|
||||
f = asyncio.Future()
|
||||
self.db_requests.put((f, func, args, kwargs))
|
||||
return f.result(timeout=10)
|
||||
return f
|
||||
return wrapper
|
||||
|
||||
class SqlDB(Logger):
|
||||
|
||||
def __init__(self, network, path, base):
|
||||
def __init__(self, network, path, base, commit_interval=None):
|
||||
Logger.__init__(self)
|
||||
self.base = base
|
||||
self.network = network
|
||||
self.path = path
|
||||
self.commit_interval = commit_interval
|
||||
self.db_requests = queue.Queue()
|
||||
self.sql_thread = threading.Thread(target=self.run_sql)
|
||||
self.sql_thread.start()
|
||||
|
||||
def run_sql(self):
|
||||
#return
|
||||
self.logger.info("SQL thread started")
|
||||
engine = create_engine('sqlite:///' + self.path, pool_reset_on_return=None, poolclass=StaticPool)#, echo=True)
|
||||
DBSession = sessionmaker(bind=engine, autoflush=False)
|
||||
self.DBSession = DBSession()
|
||||
if not os.path.exists(self.path):
|
||||
self.base.metadata.create_all(engine)
|
||||
self.DBSession = DBSession()
|
||||
i = 0
|
||||
while self.network.asyncio_loop.is_running():
|
||||
try:
|
||||
future, func, args, kwargs = self.db_requests.get(timeout=0.1)
|
||||
@@ -50,7 +55,14 @@ class SqlDB(Logger):
|
||||
except BaseException as e:
|
||||
future.set_exception(e)
|
||||
continue
|
||||
future.set_result(result)
|
||||
if not future.cancelled():
|
||||
future.set_result(result)
|
||||
# note: in sweepstore session.commit() is called inside
|
||||
# the sql-decorated methods, so commiting to disk is awaited
|
||||
if self.commit_interval:
|
||||
i = (i + 1) % self.commit_interval
|
||||
if i == 0:
|
||||
self.DBSession.commit()
|
||||
# write
|
||||
self.DBSession.commit()
|
||||
self.logger.info("SQL thread terminated")
|
||||
|
||||
Reference in New Issue
Block a user