两个带有BinancesocketManager的Websocket

发布于 2025-01-27 08:35:04 字数 497 浏览 2 评论 0原文

我正在尝试打开两个Web插座 - 深度书和用户套接字。

这是我的代码:

async def sockets(client):
    bm = BinanceSocketManager(client)
    ds = bm.depth_socket("BTCUSDT", depth=BinanceSocketManager.WEBSOCKET_DEPTH_5)
    
    print("Started...")

    async with ds as depth_socket:
        while True:
            res = await depth_socket.recv()
            print(res)

    await client.close_connection()

我需要bm.user_socket()同时打开。

我如何与Binancesocketmanager同时打开其中两个?

I'm trying to open two web sockets - depth book and user socket.

Here's my code:

async def sockets(client):
    bm = BinanceSocketManager(client)
    ds = bm.depth_socket("BTCUSDT", depth=BinanceSocketManager.WEBSOCKET_DEPTH_5)
    
    print("Started...")

    async with ds as depth_socket:
        while True:
            res = await depth_socket.recv()
            print(res)

    await client.close_connection()

I need bm.user_socket() socket to be opened as well at the same time.

How can I have two of them opened at the same time with BinanceSocketManager?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2025-02-03 08:35:04

我已经修改了您的代码,因为它不起作用。我的感觉是,您对asyncio的工作方式没有很好的了解。我建议您阅读此 tutorial> tutorial> tutorial /代码>事件循环。

您需要的是使用使用使用asyncio's suarsy_future模块,该模块将使您能够 sugch sugch tasks 和运行它们异步

我们在下面的代码中正在做的是,我们正在为depth_socketuser_socket 创建单独的任务,哪些循环循环但也异常运行,以便他们可以随时返回数据。服务器无需等待其他任务完成。

我认为您遇到的问题是您正在尝试将depth_socketuser_socket 放在同一async coroutine中,因此请与depth_socket 表示您永远无法同时循环浏览user_socket

我在上面给您的教程链接中有一个关于运行多个Coroutines的部分,这有助于我了解这是如何工作的。

不幸的是,我似乎无法连接到Binance TestNet,因此我无法测试user_socket 任务在发生用户事件时实际上有效,但是当连接时它不会丢弃错误到Livenet。让我知道您是否正在获得贸易活动。

当然,您需要输入API密钥和秘密。

import asyncio
from binance import AsyncClient, BinanceSocketManager

api_key = '...'
api_secret = '...'

async def task_depth_socket():
    client = await AsyncClient.create(api_key, api_secret)
    bm = BinanceSocketManager(client)
    ds = bm.depth_socket("BTCUSDT", depth=BinanceSocketManager.WEBSOCKET_DEPTH_5)

    async with ds as depth_socket:
        while True:
            res = await depth_socket.recv()
            print(res)
    
async def task_user_socket():
    client = await AsyncClient.create(api_key, api_secret)
    bm = BinanceSocketManager(client)
    us = bm.user_socket()
    
    async with us as user_socket:
        while True:
            res = await user_socket.recv()
            print(res)    
 
loop = asyncio.get_event_loop()

try:
    asyncio.ensure_future(task_depth_socket())
    asyncio.ensure_future(task_user_socket())
    loop.run_forever()

except KeyboardInterrupt:
    pass
finally:
    print("Closing Loop")
    loop.close()

I've amended your code because it wasn't working. My feeling is that you don't have a good understanding of how asyncio works. I would recommend you read this tutorial on asyncio event loops.

What you need is to run multiple coroutines using asyncio's ensure_future module which will enable you to schedule tasks and run them asynchronously.

What we are doing in the code below is we are creating separate tasks for depth_socket and user_socket which loop indefinately but also run asynchronously so they can return data whenever it is sent by the server without waiting for the other task to finish.

I think the problem you were having is that you were trying to put both depth_socket and user_socket in the same async coroutine so looping indefinately with depth_socket meant that you could never loop through the user_socket concurrently.

There's a section on running multiple coroutines in the tutorial link I have given you above which helped me a lot understand how this works.

Unfortunately I can't seem to connect to the binance testnet so I haven't been able to test if the user_socket task actually works when a user event occurs but it should as it's not throwing an error when connected to livenet. Let me know if you are getting trade events.

You will need to input your api key and secret of course.

import asyncio
from binance import AsyncClient, BinanceSocketManager

api_key = '...'
api_secret = '...'

async def task_depth_socket():
    client = await AsyncClient.create(api_key, api_secret)
    bm = BinanceSocketManager(client)
    ds = bm.depth_socket("BTCUSDT", depth=BinanceSocketManager.WEBSOCKET_DEPTH_5)

    async with ds as depth_socket:
        while True:
            res = await depth_socket.recv()
            print(res)
    
async def task_user_socket():
    client = await AsyncClient.create(api_key, api_secret)
    bm = BinanceSocketManager(client)
    us = bm.user_socket()
    
    async with us as user_socket:
        while True:
            res = await user_socket.recv()
            print(res)    
 
loop = asyncio.get_event_loop()

try:
    asyncio.ensure_future(task_depth_socket())
    asyncio.ensure_future(task_user_socket())
    loop.run_forever()

except KeyboardInterrupt:
    pass
finally:
    print("Closing Loop")
    loop.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文