-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from Lost-MSth/dev
Dev
- Loading branch information
Showing
20 changed files
with
478 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from atexit import register | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
from .constant import Constant | ||
from .sql import Connect | ||
|
||
|
||
class BGTask: | ||
executor = ThreadPoolExecutor(max_workers=1) | ||
|
||
def __init__(self, func, *args, **kwargs): | ||
self.future = self.executor.submit(func, *args, **kwargs) | ||
|
||
def result(self): | ||
return self.future.result() | ||
|
||
def cancel(self) -> bool: | ||
return self.future.cancel() | ||
|
||
def done(self) -> bool: | ||
return self.future.done() | ||
|
||
@staticmethod | ||
def shutdown(wait: bool = True): | ||
BGTask.executor.shutdown(wait) | ||
|
||
|
||
@register | ||
def atexit(): | ||
BGTask.shutdown() | ||
|
||
|
||
def logdb_execute_func(sql, *args, **kwargs): | ||
with Connect(Constant.SQLITE_LOG_DATABASE_PATH) as c: | ||
c.execute(sql, *args, **kwargs) | ||
|
||
|
||
def logdb_execute_many_func(sql, *args, **kwargs): | ||
with Connect(Constant.SQLITE_LOG_DATABASE_PATH) as c: | ||
c.executemany(sql, *args, **kwargs) | ||
|
||
|
||
def logdb_execute(sql: str, *args, **kwargs): | ||
'''异步执行SQL,日志库写入,注意不会直接返回结果''' | ||
return BGTask(logdb_execute_func, sql, *args, **kwargs) | ||
|
||
|
||
def logdb_execute_many(sql: str, *args, **kwargs): | ||
'''异步批量执行SQL,日志库写入,注意不会直接返回结果''' | ||
return BGTask(logdb_execute_many_func, sql, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.