mirror of
https://github.com/zhinianboke/xianyu-auto-reply.git
synced 2025-08-30 01:27:35 +08:00
提交
This commit is contained in:
parent
5b2565f6ef
commit
bf619985e7
10
Start.py
10
Start.py
@ -6,6 +6,7 @@
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import asyncio
|
||||
import threading
|
||||
import uvicorn
|
||||
@ -13,6 +14,15 @@ from urllib.parse import urlparse
|
||||
from pathlib import Path
|
||||
from loguru import logger
|
||||
|
||||
# 修复Linux环境下的asyncio子进程问题
|
||||
if sys.platform.startswith('linux'):
|
||||
try:
|
||||
# 在程序启动时就设置正确的事件循环策略
|
||||
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
||||
logger.debug("已设置事件循环策略以支持子进程")
|
||||
except Exception as e:
|
||||
logger.debug(f"设置事件循环策略失败: {e}")
|
||||
|
||||
from config import AUTO_REPLY, COOKIES_LIST
|
||||
import cookie_manager as cm
|
||||
from db_manager import db_manager
|
||||
|
@ -3282,13 +3282,62 @@ class XianyuLive:
|
||||
playwright = None
|
||||
browser = None
|
||||
try:
|
||||
import asyncio
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
logger.info(f"【{self.cookie_id}】开始通过浏览器刷新Cookie...")
|
||||
logger.info(f"【{self.cookie_id}】刷新前Cookie长度: {len(self.cookies_str)}")
|
||||
logger.info(f"【{self.cookie_id}】刷新前Cookie字段数: {len(self.cookies)}")
|
||||
|
||||
playwright = await async_playwright().start()
|
||||
# Docker环境下修复asyncio子进程问题
|
||||
is_docker = os.getenv('DOCKER_ENV') or os.path.exists('/.dockerenv')
|
||||
|
||||
if is_docker:
|
||||
logger.debug(f"【{self.cookie_id}】检测到Docker环境,应用asyncio修复")
|
||||
|
||||
# 创建一个完整的虚拟子进程监视器
|
||||
class DummyChildWatcher:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
def is_active(self):
|
||||
return True
|
||||
|
||||
def add_child_handler(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def remove_child_handler(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def attach_loop(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
def __del__(self):
|
||||
pass
|
||||
|
||||
# 创建自定义事件循环策略
|
||||
class DockerEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
|
||||
def get_child_watcher(self):
|
||||
return DummyChildWatcher()
|
||||
|
||||
# 临时设置策略
|
||||
old_policy = asyncio.get_event_loop_policy()
|
||||
asyncio.set_event_loop_policy(DockerEventLoopPolicy())
|
||||
|
||||
try:
|
||||
playwright = await async_playwright().start()
|
||||
finally:
|
||||
# 恢复原策略
|
||||
asyncio.set_event_loop_policy(old_policy)
|
||||
else:
|
||||
# 非Docker环境,正常启动
|
||||
playwright = await async_playwright().start()
|
||||
|
||||
# 启动浏览器(参照商品搜索的配置)
|
||||
browser_args = [
|
||||
|
Loading…
x
Reference in New Issue
Block a user