Qilan2 commited on
Commit
31f09c4
·
verified ·
1 Parent(s): 11983aa

Update sbx/b.py

Browse files
Files changed (1) hide show
  1. sbx/b.py +46 -1
sbx/b.py CHANGED
@@ -1,6 +1,9 @@
1
  from flask import Flask, request, jsonify, Response
2
  import os
3
  import time
 
 
 
4
 
5
  app = Flask(__name__)
6
 
@@ -36,9 +39,49 @@ def get_sub_content():
36
  except Exception as e:
37
  return jsonify({"error": str(e)}), 500
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  @app.route('/xx', methods=['GET'])
40
  def system_info():
41
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  # 获取CPU信息
43
  cpu_cores = os.cpu_count()
44
 
@@ -105,7 +148,7 @@ def system_info():
105
  # 构建格式化的中文响应
106
  response_text = f"""
107
  ============ 系统信息 ============
108
-
109
  CPU信息:
110
  核心数: {cpu_cores} 核
111
  使用率: {cpu_usage:.2f}%
@@ -125,6 +168,8 @@ CPU信息:
125
  15分钟平均: {load_avg[2]}
126
 
127
  ==================================
 
 
128
  """
129
 
130
  return Response(response_text, mimetype='text/plain; charset=utf-8')
 
1
  from flask import Flask, request, jsonify, Response
2
  import os
3
  import time
4
+ import gc
5
+ import threading
6
+ import sys
7
 
8
  app = Flask(__name__)
9
 
 
39
  except Exception as e:
40
  return jsonify({"error": str(e)}), 500
41
 
42
+ def perform_cleanup():
43
+ """执行清理操作的函数"""
44
+ # Python垃圾回收
45
+ collected = gc.collect()
46
+
47
+ # 清理Python模块缓存
48
+ for module in list(sys.modules.keys()):
49
+ if module.startswith('temp') or module.startswith('_'):
50
+ del sys.modules[module]
51
+
52
+ # 清理可能存在的临时文件
53
+ temp_dir = '/tmp'
54
+ if os.path.exists(temp_dir) and os.access(temp_dir, os.W_OK):
55
+ for f in os.listdir(temp_dir):
56
+ if f.startswith('flask-') or f.startswith('tmp'):
57
+ try:
58
+ os.remove(os.path.join(temp_dir, f))
59
+ except:
60
+ pass
61
+
62
+ return collected
63
+
64
  @app.route('/xx', methods=['GET'])
65
  def system_info():
66
  try:
67
+ # 检查是否有清理参数
68
+ cleanup = request.args.get('cleanup', 'false').lower() == 'true'
69
+
70
+ # 如果需要清理,执行清理操作
71
+ if cleanup:
72
+ # 在后台线程中执行清理,避免阻塞请求
73
+ def cleanup_task():
74
+ collected = perform_cleanup()
75
+ print(f"清理完成,回收了 {collected} 个对象")
76
+
77
+ thread = threading.Thread(target=cleanup_task)
78
+ thread.daemon = True
79
+ thread.start()
80
+
81
+ cleanup_message = "已开始执行清理操作(Python垃圾回收和临时文件清理)\n\n"
82
+ else:
83
+ cleanup_message = ""
84
+
85
  # 获取CPU信息
86
  cpu_cores = os.cpu_count()
87
 
 
148
  # 构建格式化的中文响应
149
  response_text = f"""
150
  ============ 系统信息 ============
151
+ {cleanup_message}
152
  CPU信息:
153
  核心数: {cpu_cores} 核
154
  使用率: {cpu_usage:.2f}%
 
168
  15分钟平均: {load_avg[2]}
169
 
170
  ==================================
171
+ 提示: 访问 /xx?cleanup=true 可以尝试清理Python资源
172
+ 注意: 由于权限限制,只能清理Python层面的资源
173
  """
174
 
175
  return Response(response_text, mimetype='text/plain; charset=utf-8')