fix: 内存溢出与任务调度优化
This commit is contained in:
@@ -10,12 +10,27 @@ from bs4 import BeautifulSoup
|
||||
import re
|
||||
import time
|
||||
import atexit
|
||||
import weakref
|
||||
from typing import Optional, Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
BASE_URL = "https://postoa.aidunsoft.com"
|
||||
|
||||
_api_browser_instances: "weakref.WeakSet[APIBrowser]" = weakref.WeakSet()
|
||||
|
||||
|
||||
def _cleanup_api_browser_instances():
|
||||
"""进程退出时清理残留的API浏览器实例(弱引用,不阻止GC)"""
|
||||
for inst in list(_api_browser_instances):
|
||||
try:
|
||||
inst.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
atexit.register(_cleanup_api_browser_instances)
|
||||
|
||||
|
||||
@dataclass
|
||||
class APIBrowseResult:
|
||||
@@ -52,8 +67,7 @@ class APIBrowser:
|
||||
else:
|
||||
self.proxy_server = None
|
||||
|
||||
# 注册退出清理函数
|
||||
atexit.register(self._cleanup_on_exit)
|
||||
_api_browser_instances.add(self)
|
||||
|
||||
def log(self, message: str):
|
||||
"""记录日志"""
|
||||
@@ -427,14 +441,10 @@ class APIBrowser:
|
||||
self.session.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
def _cleanup_on_exit(self):
|
||||
"""进程退出时的清理函数(由atexit调用)"""
|
||||
if not self._closed:
|
||||
finally:
|
||||
try:
|
||||
self.session.close()
|
||||
self._closed = True
|
||||
except:
|
||||
_api_browser_instances.discard(self)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def __enter__(self):
|
||||
|
||||
Reference in New Issue
Block a user