Skip to content

Commit 92247c0

Browse files
author
Taois
committed
feat: 尝试改善hipy守护进程,难度较大,也许会出现不确定的bug
1 parent 54f9fea commit 92247c0

File tree

6 files changed

+204
-90
lines changed

6 files changed

+204
-90
lines changed

docs/updateRecord.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
1. 增加一堆 `hipy` 模板源 和相关APP模板
88
2. hipy spider 增加 `setCache` `getCache` 函数
99
3. map.txt 分隔符从 `@` 改为 `@@`
10+
4. 优化hipy守护进程执行逻辑,改善性能、稳定性、内存管理
1011

1112
### 20250821
1213

libs/hipy.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const _config_path = path.join(__dirname, '../config');
1717
const _lib_path = path.join(__dirname, '../spider/py');
1818
const timeout = 30000; // 30秒超时
1919

20+
function stringify(arg) {
21+
return Array.isArray(arg) || typeof arg == "object" ? JSON.stringify(arg) : arg
22+
}
23+
2024
const json2Object = function (json) {
2125
// console.log('json2Object:', json);
2226
if (!json) {
@@ -107,13 +111,13 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
107111
`--script-path "${filePath}"`,
108112
`--method-name "${methodName}"`,
109113
`--env '${JSON.stringify(env)}'`,
110-
...args.map(arg => `--arg '${JSON.stringify(arg)}'`)
114+
...args.map(arg => `--arg '${stringify(arg)}'`)
111115
].join(' ');
112116
// console.log(command);
113117
const cmd_args = [];
114118
args.forEach(arg => {
115119
cmd_args.push(`--arg`);
116-
cmd_args.push(`${JSON.stringify(arg)}`);
120+
cmd_args.push(`${stringify(arg)}`);
117121
});
118122
const options = {
119123
mode: 'text',
@@ -165,7 +169,7 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
165169
// 定义Spider类的方法
166170
const spiderMethods = [
167171
'init', 'home', 'homeVod', 'homeContent', 'category',
168-
'detail', 'search', 'play', 'proxy', 'action', 'initEnv'
172+
'detail', 'search', 'play', 'proxy', 'action'
169173
];
170174

171175
// 为代理对象添加方法
@@ -175,11 +179,6 @@ const loadEsmWithHash = async function (filePath, fileHash, env) {
175179
};
176180
});
177181

178-
// 处理initEnv调用
179-
if (typeof spiderProxy.initEnv === 'function' && env) {
180-
await spiderProxy.initEnv(env);
181-
}
182-
183182
return spiderProxy;
184183
}
185184

@@ -194,12 +193,12 @@ const init = async function (filePath, env = {}, refresh) {
194193
const fileHash = computeHash(fileContent);
195194
const moduleName = path.basename(filePath, '.js');
196195
let moduleExt = env.ext || '';
197-
const default_init_cfg = {
198-
stype: 4, //T3/T4 源类型
199-
skey: `hipy_${moduleName}`,
200-
sourceKey: `hipy_${moduleName}`,
201-
ext: moduleExt,
202-
};
196+
// const default_init_cfg = { // T3才需要这种结构
197+
// stype: 4, //T3/T4 源类型
198+
// skey: `hipy_${moduleName}`,
199+
// sourceKey: `hipy_${moduleName}`,
200+
// ext: moduleExt,
201+
// };
203202
let SitesMap = getSitesMap(_config_path);
204203
if (moduleExt && SitesMap[moduleName]) {
205204
try {
@@ -226,7 +225,8 @@ const init = async function (filePath, env = {}, refresh) {
226225
module = await loadEsmWithHash(filePath, fileHash, env);
227226
// console.log('module:', module);
228227
const rule = module;
229-
const initValue = await rule.init(default_init_cfg) || {};
228+
// const initValue = await rule.init(default_init_cfg) || {};
229+
const initValue = await rule.init(moduleExt) || {};
230230
let t2 = getNowTime();
231231
const moduleObject = deepCopy(rule);
232232
moduleObject.cost = t2 - t1;

spider/py/base/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ ujson
55
pyquery
66
jsonpath
77
json5
8-
jpype1
8+
jpype1
9+
pympler

spider/py/base/spider.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def __init__(self, query_params=None, t4_api=None):
5151
self.t4_api = t4_api or ''
5252
self.extend = ''
5353
self.ENV = _ENV
54-
# self.log(f't4_api:{t4_api}')
5554
self._cache = {}
55+
self.log(f'BaseSpider __init__ t4_api:{t4_api}')
5656

5757
def __new__(cls, *args, **kwargs):
5858
if cls._instance:
@@ -106,19 +106,13 @@ def isVideoFormat(self, url):
106106
def manualVideoCheck(self):
107107
pass
108108

109-
@abstractmethod
109+
# @abstractmethod
110110
def getName(self):
111-
pass
111+
return 'BaseSpider'
112112

113113
def init_api_ext_file(self):
114114
pass
115115

116-
def initEnv(self, env=None):
117-
if env is None:
118-
env = {}
119-
self._ENV = env
120-
self.t4_api = env.get('proxyUrl')
121-
122116
def getProxyUrl(self):
123117
"""
124118
获取本地代理地址

0 commit comments

Comments
 (0)