mirror of
https://github.com/zhinianboke/xianyu-auto-reply.git
synced 2025-08-31 09:48:48 +08:00
提交
This commit is contained in:
parent
5b2565f6ef
commit
bf619985e7
10
Start.py
10
Start.py
@ -6,6 +6,7 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import threading
|
||||||
import uvicorn
|
import uvicorn
|
||||||
@ -13,6 +14,15 @@ from urllib.parse import urlparse
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from loguru import logger
|
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
|
from config import AUTO_REPLY, COOKIES_LIST
|
||||||
import cookie_manager as cm
|
import cookie_manager as cm
|
||||||
from db_manager import db_manager
|
from db_manager import db_manager
|
||||||
|
@ -3282,12 +3282,61 @@ class XianyuLive:
|
|||||||
playwright = None
|
playwright = None
|
||||||
browser = None
|
browser = None
|
||||||
try:
|
try:
|
||||||
|
import asyncio
|
||||||
from playwright.async_api import async_playwright
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
logger.info(f"【{self.cookie_id}】开始通过浏览器刷新Cookie...")
|
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_str)}")
|
||||||
logger.info(f"【{self.cookie_id}】刷新前Cookie字段数: {len(self.cookies)}")
|
logger.info(f"【{self.cookie_id}】刷新前Cookie字段数: {len(self.cookies)}")
|
||||||
|
|
||||||
|
# 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()
|
playwright = await async_playwright().start()
|
||||||
|
|
||||||
# 启动浏览器(参照商品搜索的配置)
|
# 启动浏览器(参照商品搜索的配置)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user