Spaces:
BG5
/
Running

BG5 commited on
Commit
d6c8a43
·
verified ·
1 Parent(s): 60fd002

Update fun.py

Browse files
Files changed (1) hide show
  1. fun.py +21 -17
fun.py CHANGED
@@ -20,7 +20,11 @@ BROWSERS: Dict[str, dict] = {} # {browser_id: {"process": Popen, "port": int, "
20
  PORT_RANGE = (9300, 9700)
21
  CHROME_PATH = "google-chrome" # Linux下可用命令,或自定义绝对路径
22
  PROFILE_BASE = "/tmp/profiles"
23
-
 
 
 
 
24
  def get_free_port():
25
  used_ports = {b["port"] for b in BROWSERS.values()}
26
  for _ in range(100):
@@ -74,19 +78,19 @@ async def open_browser(request: Request):
74
  # 加入默认参数
75
  logger.info(f"使用默认参数: {get_default_launch_args()}")
76
  args.extend(get_default_launch_args())
77
- # if launch_args:
78
- # # 拆分参数并去重,保留顺序
79
- # base_args = args[1:] # 除去chrome可执行文件
80
- # extra_args = [a for a in launch_args.split() if a]
81
- # all_args = base_args + extra_args
82
- # seen = set()
83
- # deduped_args = []
84
- # for arg in all_args:
85
- # key = arg.split('=')[0].strip() if '=' in arg else arg.strip()
86
- # if key not in seen:
87
- # seen.add(key)
88
- # deduped_args.append(arg)
89
- # args = [CHROME_PATH] + deduped_args
90
  proc = subprocess.Popen(args)
91
  if not wait_port("127.0.0.1", port, timeout=60):
92
  proc.terminate()
@@ -208,11 +212,11 @@ async def find_browser_by_target_id(target_id: str):
208
  @app.api_route("/json/version", methods=["GET"])
209
  @app.api_route("/json/protocol", methods=["GET"])
210
  async def cdp_native_proxy(request: Request):
211
- # 这个接口只返回第一个浏览器实例的内容
212
- # 1. 获取第一个浏览器实例
213
  if not BROWSERS:
214
  return Response(content="No browser instance", status_code=404)
215
- browser_id = next(iter(BROWSERS.keys()))
216
  b = get_browser_by_id(browser_id)
217
  if not b:
218
  return Response(content="browser not found", status_code=404)
 
20
  PORT_RANGE = (9300, 9700)
21
  CHROME_PATH = "google-chrome" # Linux下可用命令,或自定义绝对路径
22
  PROFILE_BASE = "/tmp/profiles"
23
+ def get_system_usage():
24
+ cpu = subprocess.check_output("top -l 1 | grep -E '^CPU'", shell=True)
25
+ mem = subprocess.check_output("vm_stat", shell=True) # macOS
26
+ # Linux 可用: free -m
27
+ print("系统资源:", cpu.decode().strip())
28
  def get_free_port():
29
  used_ports = {b["port"] for b in BROWSERS.values()}
30
  for _ in range(100):
 
78
  # 加入默认参数
79
  logger.info(f"使用默认参数: {get_default_launch_args()}")
80
  args.extend(get_default_launch_args())
81
+ if launch_args:
82
+ # 拆分参数并去重,保留顺序
83
+ base_args = args[1:] # 除去chrome可执行文件
84
+ extra_args = [a for a in launch_args.split() if a]
85
+ all_args = base_args + extra_args
86
+ seen = set()
87
+ deduped_args = []
88
+ for arg in all_args:
89
+ key = arg.split('=')[0].strip() if '=' in arg else arg.strip()
90
+ if key not in seen:
91
+ seen.add(key)
92
+ deduped_args.append(arg)
93
+ args = [CHROME_PATH] + deduped_args
94
  proc = subprocess.Popen(args)
95
  if not wait_port("127.0.0.1", port, timeout=60):
96
  proc.terminate()
 
212
  @app.api_route("/json/version", methods=["GET"])
213
  @app.api_route("/json/protocol", methods=["GET"])
214
  async def cdp_native_proxy(request: Request):
215
+ # 这个接口只返回最后一个浏览器实例的内容
216
+ # 1. 获取最后一个浏览器实例
217
  if not BROWSERS:
218
  return Response(content="No browser instance", status_code=404)
219
+ browser_id = next(reversed(BROWSERS.keys()))
220
  b = get_browser_by_id(browser_id)
221
  if not b:
222
  return Response(content="browser not found", status_code=404)