made connections thread safe
This commit is contained in:
parent
83889b2941
commit
c2ee013975
7
db.py
7
db.py
|
@ -4,6 +4,7 @@ The bot's database connection class.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import threading
|
||||||
|
|
||||||
class FulviaDB(object):
|
class FulviaDB(object):
|
||||||
"""
|
"""
|
||||||
|
@ -13,6 +14,7 @@ class FulviaDB(object):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
path = config.core.db_filename
|
path = config.core.db_filename
|
||||||
self.filename = path
|
self.filename = path
|
||||||
|
self.db_lock = threading.Lock()
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
"""Return a raw database connection object."""
|
"""Return a raw database connection object."""
|
||||||
|
@ -26,5 +28,8 @@ class FulviaDB(object):
|
||||||
called per PEP 249.
|
called per PEP 249.
|
||||||
"""
|
"""
|
||||||
with self.connect() as conn:
|
with self.connect() as conn:
|
||||||
|
self.db_lock.acquire()
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
return cur.execute(*args, **kwargs)
|
res = cur.execute(*args, **kwargs)
|
||||||
|
self.db_lock.release()
|
||||||
|
return res
|
||||||
|
|
Loading…
Reference in New Issue
Block a user