add
This commit is contained in:
parent
9eff208e8e
commit
c103fa700e
@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import platform
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List
|
||||||
import ccxt.async_support as ccxt
|
import ccxt.async_support as ccxt
|
||||||
import mcp.types as types
|
import mcp.types as types
|
||||||
@ -6,9 +7,10 @@ from mcp.server import Server, NotificationOptions
|
|||||||
from mcp.server.models import InitializationOptions
|
from mcp.server.models import InitializationOptions
|
||||||
import mcp.server.stdio
|
import mcp.server.stdio
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import sys
|
|
||||||
import platform
|
# Windows平台特定的事件循环策略设置
|
||||||
import signal
|
if platform.system() == 'Windows':
|
||||||
|
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||||
|
|
||||||
# 初始化服务器
|
# 初始化服务器
|
||||||
server = Server("crypto-server")
|
server = Server("crypto-server")
|
||||||
@ -425,58 +427,10 @@ async def main():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def handle_sigint():
|
|
||||||
"""Handle keyboard interrupt"""
|
|
||||||
for instance in exchange_instances.values():
|
|
||||||
asyncio.create_task(instance.close())
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
def configure_asyncio_event_loop():
|
|
||||||
"""Configure the appropriate event loop based on the platform"""
|
|
||||||
if platform.system() == 'Windows':
|
|
||||||
# Windows specific configuration for aiodns compatibility
|
|
||||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
else:
|
|
||||||
# Unix-like systems configuration
|
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
|
|
||||||
# Set up signal handlers
|
|
||||||
try:
|
|
||||||
if platform.system() != 'Windows':
|
|
||||||
# Unix-like systems support these signals
|
|
||||||
loop.add_signal_handler(signal.SIGINT, handle_sigint)
|
|
||||||
loop.add_signal_handler(signal.SIGTERM, handle_sigint)
|
|
||||||
else:
|
|
||||||
# Windows alternative
|
|
||||||
signal.signal(signal.SIGINT, lambda x, y: handle_sigint())
|
|
||||||
except NotImplementedError:
|
|
||||||
# Fallback for environments where signals are not supported
|
|
||||||
pass
|
|
||||||
|
|
||||||
return loop
|
|
||||||
|
|
||||||
def run_server():
|
def run_server():
|
||||||
"""Wrapper to run the async main function with proper event loop configuration"""
|
"""Wrapper to run the async main function"""
|
||||||
try:
|
asyncio.run(main())
|
||||||
loop = configure_asyncio_event_loop()
|
|
||||||
loop.run_until_complete(main())
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
handle_sigint()
|
|
||||||
finally:
|
|
||||||
try:
|
|
||||||
# Clean up pending tasks
|
|
||||||
pending = asyncio.all_tasks(loop)
|
|
||||||
for task in pending:
|
|
||||||
task.cancel()
|
|
||||||
# Give tasks a chance to clean up
|
|
||||||
if pending:
|
|
||||||
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
|
|
||||||
finally:
|
|
||||||
loop.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
run_server()
|
asyncio.run(main())
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user