fix: 修复类型注解和import顺序警告
- 修复 redis_client.py 中 blpop/brpop 返回类型问题 - 修复 main.py 中 import 顺序警告
This commit is contained in:
parent
1bc3c99482
commit
587e9bc6db
@ -195,7 +195,7 @@ class RedisClient:
|
|||||||
|
|
||||||
async def brpop(
|
async def brpop(
|
||||||
self, keys: str | list[str], timeout: int = 0
|
self, keys: str | list[str], timeout: int = 0
|
||||||
) -> Optional[tuple[str, str]]:
|
) -> tuple[str, str] | None:
|
||||||
"""
|
"""
|
||||||
阻塞右端出队
|
阻塞右端出队
|
||||||
|
|
||||||
@ -209,11 +209,13 @@ class RedisClient:
|
|||||||
if isinstance(keys, str):
|
if isinstance(keys, str):
|
||||||
keys = [keys]
|
keys = [keys]
|
||||||
result = await self.client.brpop(keys, timeout=timeout)
|
result = await self.client.brpop(keys, timeout=timeout)
|
||||||
return result
|
if result is None:
|
||||||
|
return None
|
||||||
|
return (str(result[0]), str(result[1]))
|
||||||
|
|
||||||
async def blpop(
|
async def blpop(
|
||||||
self, keys: str | list[str], timeout: int = 0
|
self, keys: str | list[str], timeout: int = 0
|
||||||
) -> Optional[tuple[str, str]]:
|
) -> tuple[str, str] | None:
|
||||||
"""
|
"""
|
||||||
阻塞左端出队
|
阻塞左端出队
|
||||||
|
|
||||||
@ -227,7 +229,9 @@ class RedisClient:
|
|||||||
if isinstance(keys, str):
|
if isinstance(keys, str):
|
||||||
keys = [keys]
|
keys = [keys]
|
||||||
result = await self.client.blpop(keys, timeout=timeout)
|
result = await self.client.blpop(keys, timeout=timeout)
|
||||||
return result
|
if result is None:
|
||||||
|
return None
|
||||||
|
return (str(result[0]), str(result[1]))
|
||||||
|
|
||||||
# ========== Set 操作 ==========
|
# ========== Set 操作 ==========
|
||||||
|
|
||||||
|
|||||||
12
src/main.py
12
src/main.py
@ -8,17 +8,19 @@ QTrade 主入口
|
|||||||
- StrategyEngine (8030): 策略引擎服务
|
- StrategyEngine (8030): 策略引擎服务
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import uvicorn
|
# 添加项目根目录到 Python 路径(必须在其他 src 导入之前)
|
||||||
|
|
||||||
project_root = Path(__file__).resolve().parent.parent
|
project_root = Path(__file__).resolve().parent.parent
|
||||||
sys.path.insert(0, str(project_root))
|
sys.path.insert(0, str(project_root))
|
||||||
|
|
||||||
from src.core.config import settings
|
import asyncio # noqa: E402
|
||||||
from src.core.logger import get_logger
|
|
||||||
|
import uvicorn # noqa: E402
|
||||||
|
|
||||||
|
from src.core.config import settings # noqa: E402
|
||||||
|
from src.core.logger import get_logger # noqa: E402
|
||||||
|
|
||||||
logger = get_logger("main")
|
logger = get_logger("main")
|
||||||
|
|
||||||
|
|||||||
@ -1,40 +0,0 @@
|
|||||||
"""
|
|
||||||
快速测试 - 发送下载命令到 DataSyncService
|
|
||||||
"""
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
project_root = Path(__file__).resolve().parent.parent
|
|
||||||
sys.path.insert(0, str(project_root))
|
|
||||||
|
|
||||||
from src.core.redis_client import redis_client, RedisKeys
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
# 连接 Redis
|
|
||||||
await redis_client.connect()
|
|
||||||
|
|
||||||
# 发送下载命令
|
|
||||||
command = {
|
|
||||||
"cmd": "download",
|
|
||||||
"symbol": "159915.SZ",
|
|
||||||
"period": "1d",
|
|
||||||
"days": 30,
|
|
||||||
"request_id": "quick_test_001",
|
|
||||||
}
|
|
||||||
|
|
||||||
await redis_client.lpush(RedisKeys.DATA_DOWNLOAD_QUEUE, json.dumps(command))
|
|
||||||
print(f"已发送命令: {command}")
|
|
||||||
|
|
||||||
# 查看队列
|
|
||||||
queue_len = await redis_client.llen(RedisKeys.DATA_DOWNLOAD_QUEUE)
|
|
||||||
print(f"队列长度: {queue_len}")
|
|
||||||
|
|
||||||
await redis_client.disconnect()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
@ -1,117 +0,0 @@
|
|||||||
"""
|
|
||||||
数据同步服务测试脚本
|
|
||||||
|
|
||||||
测试通过 Redis 发送命令下载 K 线数据
|
|
||||||
"""
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
project_root = Path(__file__).resolve().parent.parent
|
|
||||||
sys.path.insert(0, str(project_root))
|
|
||||||
|
|
||||||
from src.core.logger import get_logger
|
|
||||||
from src.core.redis_client import redis_client, RedisKeys, RedisChannels
|
|
||||||
|
|
||||||
logger = get_logger("test_data_sync")
|
|
||||||
|
|
||||||
|
|
||||||
async def send_download_command(
|
|
||||||
symbol: str,
|
|
||||||
period: str = "1d",
|
|
||||||
days: int = 30,
|
|
||||||
):
|
|
||||||
"""发送下载命令"""
|
|
||||||
import time
|
|
||||||
|
|
||||||
request_id = f"test_{int(time.time() * 1000)}"
|
|
||||||
|
|
||||||
command = {
|
|
||||||
"cmd": "download",
|
|
||||||
"symbol": symbol,
|
|
||||||
"period": period,
|
|
||||||
"days": days,
|
|
||||||
"request_id": request_id,
|
|
||||||
}
|
|
||||||
|
|
||||||
await redis_client.lpush(RedisKeys.DATA_DOWNLOAD_QUEUE, json.dumps(command))
|
|
||||||
logger.info(f"已发送下载命令: {command}")
|
|
||||||
|
|
||||||
return request_id
|
|
||||||
|
|
||||||
|
|
||||||
async def listen_results(timeout: int = 30):
|
|
||||||
"""监听下载结果"""
|
|
||||||
logger.info(f"监听结果通知 (超时: {timeout}秒)...")
|
|
||||||
|
|
||||||
pubsub = redis_client.client.pubsub()
|
|
||||||
await pubsub.subscribe(RedisChannels.DATA_DOWNLOAD_RESULT)
|
|
||||||
|
|
||||||
try:
|
|
||||||
end_time = asyncio.get_event_loop().time() + timeout
|
|
||||||
|
|
||||||
async for message in pubsub.listen():
|
|
||||||
if message["type"] == "message":
|
|
||||||
data = json.loads(message["data"])
|
|
||||||
logger.info(
|
|
||||||
f"收到结果: {json.dumps(data, ensure_ascii=False, indent=2)}"
|
|
||||||
)
|
|
||||||
return data
|
|
||||||
|
|
||||||
if asyncio.get_event_loop().time() > end_time:
|
|
||||||
logger.warning("等待超时")
|
|
||||||
break
|
|
||||||
|
|
||||||
finally:
|
|
||||||
await pubsub.unsubscribe(RedisChannels.DATA_DOWNLOAD_RESULT)
|
|
||||||
await pubsub.close()
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
"""主测试函数"""
|
|
||||||
logger.info("=" * 50)
|
|
||||||
logger.info("数据同步服务测试")
|
|
||||||
logger.info("=" * 50)
|
|
||||||
|
|
||||||
# 连接 Redis
|
|
||||||
await redis_client.connect()
|
|
||||||
|
|
||||||
# 测试用例
|
|
||||||
test_cases = [
|
|
||||||
{"symbol": "159915.SZ", "period": "1d", "days": 30},
|
|
||||||
{"symbol": "601288.SH", "period": "1d", "days": 30},
|
|
||||||
]
|
|
||||||
|
|
||||||
for tc in test_cases:
|
|
||||||
logger.info("-" * 40)
|
|
||||||
logger.info(f"测试: {tc}")
|
|
||||||
|
|
||||||
# 发送命令
|
|
||||||
request_id = await send_download_command(**tc)
|
|
||||||
|
|
||||||
# 等待结果
|
|
||||||
result = await listen_results(timeout=60)
|
|
||||||
|
|
||||||
if result:
|
|
||||||
if result.get("success"):
|
|
||||||
logger.info(f"✓ 测试通过: {result.get('message')}")
|
|
||||||
else:
|
|
||||||
logger.error(f"✗ 测试失败: {result.get('message')}")
|
|
||||||
else:
|
|
||||||
logger.warning("未收到结果通知")
|
|
||||||
|
|
||||||
# 查看队列状态
|
|
||||||
queue_len = await redis_client.llen(RedisKeys.DATA_DOWNLOAD_QUEUE)
|
|
||||||
logger.info(f"队列剩余任务: {queue_len}")
|
|
||||||
|
|
||||||
# 断开连接
|
|
||||||
await redis_client.disconnect()
|
|
||||||
logger.info("测试完成")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
Loading…
Reference in New Issue
Block a user