music-kraken-core/music_kraken/__init__.py

66 lines
1.7 KiB
Python
Raw Normal View History

2022-11-23 07:24:05 +00:00
import logging
2023-04-15 15:17:33 +00:00
import gc
2023-10-12 17:24:35 +00:00
import sys
2024-02-28 13:27:35 +00:00
from pathlib import Path
from rich.logging import RichHandler
from rich.console import Console
2023-04-15 15:17:33 +00:00
2024-01-16 12:55:24 +00:00
from .utils.shared import DEBUG, DEBUG_LOGGING
2023-09-10 14:27:09 +00:00
from .utils.config import logging_settings, main_settings, read_config
2023-09-10 14:27:09 +00:00
read_config()
2024-02-28 13:27:35 +00:00
2024-04-09 08:32:17 +00:00
console: Console = Console()
2024-02-28 13:27:35 +00:00
def init_logging():
log_file = main_settings['log_file']
if log_file.is_file():
last_log_file = Path(log_file.parent, "prev." + log_file.name)
with log_file.open("r", encoding="utf-8") as current_file:
with last_log_file.open("w", encoding="utf-8") as last_file:
last_file.write(current_file.read())
rich_handler = RichHandler(rich_tracebacks=True, console=console)
rich_handler.setLevel(logging_settings['log_level'] if not DEBUG_LOGGING else logging.DEBUG)
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.DEBUG)
# configure logger default
logging.basicConfig(
level=logging.DEBUG,
format=logging_settings['logging_format'],
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[
file_handler,
rich_handler,
]
)
init_logging()
from . import cli
2023-04-18 07:02:03 +00:00
if DEBUG:
2023-10-24 15:41:42 +00:00
sys.setrecursionlimit(100)
2023-09-10 14:27:09 +00:00
2024-01-16 09:37:22 +00:00
2023-09-10 14:27:09 +00:00
if main_settings['modify_gc']:
2023-04-04 20:07:56 +00:00
"""
At the start I modify the garbage collector to run a bit fewer times.
This should increase speed:
https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/
"""
# Clean up what might be garbage so far.
gc.collect(2)
allocs, gen1, gen2 = gc.get_threshold()
allocs = 50_000 # Start the GC sequence every 50K not 700 allocations.
gen1 = gen1 * 2
gen2 = gen2 * 2
gc.set_threshold(allocs, gen1, gen2)
2024-02-28 13:27:35 +00:00