提交 f89eaa07 编写于 作者: H hjdhnx

修复drpy聚搜线程池内不支持环境变量的bug

上级 17d679c9
无法预览此类型文件
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# Date : 2022/9/6 # Date : 2022/9/6
import json import json
from flask import Blueprint,abort,request,render_template,render_template_string,jsonify,make_response,redirect from flask import Blueprint,abort,request,render_template,render_template_string,jsonify,make_response,redirect,current_app
from time import time from time import time
from utils.web import getParmas,get_interval from utils.web import getParmas,get_interval
from utils.cfg import cfg from utils.cfg import cfg
...@@ -46,7 +46,7 @@ def search_one_py(rule, wd, before: str = ''): ...@@ -46,7 +46,7 @@ def search_one_py(rule, wd, before: str = ''):
print(f'{rule}发生错误:{e}') print(f'{rule}发生错误:{e}')
return None return None
def search_one(rule, wd, before: str = ''): def search_one(rule, wd, before: str = '',env:dict=None,app=None):
t1 = time() t1 = time()
if not before: if not before:
with open('js/模板.js', encoding='utf-8') as f: with open('js/模板.js', encoding='utf-8') as f:
...@@ -57,9 +57,12 @@ def search_one(rule, wd, before: str = ''): ...@@ -57,9 +57,12 @@ def search_one(rule, wd, before: str = ''):
try: try:
with open(js_path, encoding='utf-8') as f2: with open(js_path, encoding='utf-8') as f2:
jscode = f2.read() jscode = f2.read()
env = get_env()
if env: if env:
# 渲染字符串文本 render_template_string 必须带 flask的上下文
with app.app_context():
jscode = render_template_string(jscode, **env) jscode = render_template_string(jscode, **env)
# if '007' in rule:
# print(rule,jscode)
jscode = before + jscode + end_code jscode = before + jscode + end_code
# print(jscode) # print(jscode)
ctx.eval(jscode) ctx.eval(jscode)
...@@ -139,8 +142,14 @@ def merged_hide(merged_rules): ...@@ -139,8 +142,14 @@ def merged_hide(merged_rules):
logger.info(f'数据库筛选隐藏规则耗时{get_interval(t1)}毫秒,共计{all_cnt}条规则,隐藏后可渲染{len(merged_rules)}条规则') logger.info(f'数据库筛选隐藏规则耗时{get_interval(t1)}毫秒,共计{all_cnt}条规则,隐藏后可渲染{len(merged_rules)}条规则')
return merged_rules return merged_rules
def disable_exit_for_threadpool_executor():
import atexit
import concurrent.futures
atexit.unregister(concurrent.futures.thread._python_exit)
def multi_search(wd): def multi_search(wd):
lsg = storage_service() lsg = storage_service()
env = get_env()
t1 = time() t1 = time()
try: try:
timeout = round(int(lsg.getItem('SEARCH_TIMEOUT',5000))/1000,2) timeout = round(int(lsg.getItem('SEARCH_TIMEOUT',5000))/1000,2)
...@@ -167,7 +176,7 @@ def multi_search(wd): ...@@ -167,7 +176,7 @@ def multi_search(wd):
with ThreadPoolExecutor(max_workers=len(search_sites)) as executor: with ThreadPoolExecutor(max_workers=len(search_sites)) as executor:
to_do = [] to_do = []
for site in search_sites: for site in search_sites:
future = executor.submit(search_one, site, wd, before) future = executor.submit(search_one, site, wd, before,env,current_app._get_current_object())
to_do.append(future) to_do.append(future)
try: try:
for future in as_completed(to_do, timeout=timeout): # 并发执行 for future in as_completed(to_do, timeout=timeout): # 并发执行
...@@ -180,6 +189,8 @@ def multi_search(wd): ...@@ -180,6 +189,8 @@ def multi_search(wd):
import atexit import atexit
atexit.unregister(thread._python_exit) atexit.unregister(thread._python_exit)
executor.shutdown = lambda wait: None executor.shutdown = lambda wait: None
# disable_exit_for_threadpool_executor()
logger.info(f'drpy聚搜{len(search_sites)}个源共计耗时{get_interval(t1)}毫秒') logger.info(f'drpy聚搜{len(search_sites)}个源共计耗时{get_interval(t1)}毫秒')
return jsonify({ return jsonify({
"list": res "list": res
...@@ -323,7 +334,7 @@ def vod_home(): ...@@ -323,7 +334,7 @@ def vod_home():
return jsonify(data) return jsonify(data)
if wd: # 搜索 if wd: # 搜索
if rule == 'drpy': if rule == 'drpy':
# print(f'准备单独处理聚合搜索:{wd}') print(f'准备单独处理聚合搜索:{wd}')
return multi_search(wd) return multi_search(wd)
# return multi_search2(wd) # return multi_search2(wd)
else: else:
......
3.9.22beta1 3.9.22beta2
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册