Pull "is main thread" logic out to utils, force Database to be accessed on main
thread
This commit is contained in:
parent
5eceb5655c
commit
d627ed49e2
3 changed files with 10 additions and 3 deletions
|
@ -1,5 +1,5 @@
|
||||||
import json, os, sqlite3, threading, time, typing
|
import json, os, sqlite3, threading, time, typing
|
||||||
from src import Logging
|
from src import Logging, utils
|
||||||
|
|
||||||
sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v)))
|
sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v)))
|
||||||
|
|
||||||
|
@ -309,6 +309,9 @@ class Database(object):
|
||||||
def _execute_fetch(self, query: str,
|
def _execute_fetch(self, query: str,
|
||||||
fetch_func: typing.Callable[[sqlite3.Cursor], typing.Any],
|
fetch_func: typing.Callable[[sqlite3.Cursor], typing.Any],
|
||||||
params: typing.List=[]):
|
params: typing.List=[]):
|
||||||
|
if not utils.is_main_thread():
|
||||||
|
raise RuntimeError("Can't access Database outside of main thread")
|
||||||
|
|
||||||
printable_query = " ".join(query.split())
|
printable_query = " ".join(query.split())
|
||||||
start = time.monotonic()
|
start = time.monotonic()
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,8 @@ class Bot(object):
|
||||||
func: typing.Optional[typing.Callable[[], typing.Any]]=None
|
func: typing.Optional[typing.Callable[[], typing.Any]]=None
|
||||||
) -> typing.Any:
|
) -> typing.Any:
|
||||||
func = func or (lambda: None)
|
func = func or (lambda: None)
|
||||||
if threading.current_thread() is threading.main_thread():
|
|
||||||
|
if utils.is_main_thread():
|
||||||
returned = func()
|
returned = func()
|
||||||
self._trigger_client.send(b"TRIGGER")
|
self._trigger_client.send(b"TRIGGER")
|
||||||
return returned
|
return returned
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import datetime, decimal, enum, io, ipaddress, re, typing
|
import datetime, decimal, enum, io, ipaddress, re, threading, typing
|
||||||
from src.utils import cli, consts, irc, http, parse, security
|
from src.utils import cli, consts, irc, http, parse, security
|
||||||
|
|
||||||
class Direction(enum.Enum):
|
class Direction(enum.Enum):
|
||||||
|
@ -199,3 +199,6 @@ def is_ip(s: str) -> bool:
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def is_main_thread() -> bool:
|
||||||
|
return threading.current_thread() is threading.main_thread()
|
||||||
|
|
Loading…
Reference in a new issue