2025-03-21 00:55:10 +08:00
|
|
|
from loguru import logger
|
|
|
|
logger.remove()
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
logger.add(sys.stdout,
|
|
|
|
filter=lambda record: record["extra"].get("name") == "runtime")
|
|
|
|
logger.add("log/runtime.log",
|
|
|
|
rotation="100 MB",
|
|
|
|
encoding="utf-8",
|
|
|
|
filter=lambda record: record["extra"].get("name") == "runtime")
|
|
|
|
|
|
|
|
log = logger.bind(name="runtime")
|
|
|
|
|
2025-03-19 20:26:49 +08:00
|
|
|
from DrissionPage import Chromium, ChromiumOptions, SessionPage
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
import atexit
|
2025-03-20 00:52:31 +08:00
|
|
|
|
|
|
|
import listener.console
|
|
|
|
import listener.network
|
|
|
|
|
2025-03-21 00:55:10 +08:00
|
|
|
# 初始化浏览器
|
2025-03-19 20:26:49 +08:00
|
|
|
options = ChromiumOptions()
|
|
|
|
# 无头模式
|
|
|
|
# options.headless()
|
|
|
|
|
|
|
|
browser = Chromium(options)
|
|
|
|
tab = browser.latest_tab
|
|
|
|
|
2025-03-21 00:55:10 +08:00
|
|
|
import os
|
|
|
|
# 初始化线程池
|
|
|
|
cpu_count = os.cpu_count()
|
|
|
|
cpu_count = cpu_count if cpu_count is not None else 1
|
|
|
|
executor = ThreadPoolExecutor(max_workers=cpu_count * 2)
|
2025-03-20 00:52:31 +08:00
|
|
|
executor.submit(listener.console.console_listener, tab)
|
|
|
|
executor.submit(listener.network.network_listener, tab)
|
|
|
|
|
2025-03-19 20:26:49 +08:00
|
|
|
def exit():
|
2025-03-21 00:55:10 +08:00
|
|
|
log.info('退出')
|
2025-03-19 20:26:49 +08:00
|
|
|
browser.quit()
|
2025-03-21 00:55:10 +08:00
|
|
|
executor.shutdown(False)
|
2025-03-19 20:26:49 +08:00
|
|
|
|
|
|
|
atexit.register(exit)
|
2025-03-21 00:55:10 +08:00
|
|
|
|
|
|
|
from worker import run_task;
|
|
|
|
try:
|
|
|
|
run_task(browser, executor, log)
|
|
|
|
except Exception as e:
|
|
|
|
log.error(e)
|