两个带有BinancesocketManager的Websocket
我正在尝试打开两个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经修改了您的代码,因为它不起作用。我的感觉是,您对
asyncio
的工作方式没有很好的了解。我建议您阅读此 tutorial> tutorial> tutorial /代码>事件循环。您需要的是使用使用使用
asyncio
'ssuarsy_future
模块,该模块将使您能够 sugch sugch tasks 和运行它们异步。我们在下面的代码中正在做的是,我们正在为
depth_socket
和user_socket
创建单独的任务,哪些循环循环但也异常运行,以便他们可以随时返回数据。服务器无需等待其他任务完成。我认为您遇到的问题是您正在尝试将
depth_socket
和user_socket
放在同一async
coroutine中,因此请与depth_socket 表示您永远无法同时循环浏览
user_socket
。我在上面给您的教程链接中有一个关于运行多个Coroutines的部分,这有助于我了解这是如何工作的。
不幸的是,我似乎无法连接到Binance TestNet,因此我无法测试
user_socket 任务在发生用户事件时实际上有效,但是当连接时它不会丢弃错误到Livenet。让我知道您是否正在获得贸易活动。
当然,您需要输入API密钥和秘密。
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 onasyncio
event loops.What you need is to run multiple coroutines using
asyncio
'sensure_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
anduser_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
anduser_socket
in the sameasync
coroutine so looping indefinately withdepth_socket
meant that you could never loop through theuser_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.