提交 f89eaa07 编写于 作者: H hjdhnx

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

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