【发布时间】:2021-10-05 18:19:31
【问题描述】:
目前正在开展一个项目,该项目使用 Binance Websockets 获取硬币价格数据并使用数据进行计算。
我计算的硬币数量是动态的(从 150 到 ~250 不等)。
ASYNC 函数调用calculate(client, queue, coin)
它采用以下参数:
- 客户端:币安异步客户端(python-binance 库)
- 队列:如果计算结果为正,则机会进入队列
- coin:硬币,例如比特币
calculate 函数包含一个while 1(不定式)循环,因此计算会继续运行。
目前main 函数使用 asyncio.gather 来组合不同硬币 (150 - ~250) 的计算函数
async def main(queue):
await asyncio.gather(
calculate(client, queue, 'BTC'),
calculate(client, queue, 'ETH'),
calculate(client, queue, 'BNB'),
calculate(client, queue, 'LINK'),
calculate(client, queue, 'SHIB'),
calculate(client, queue, 'ADA'),
calculate(client, queue, 'XRP'),
calculate(client, queue, 'VET')
# This continues for the the other +100 coins
)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main(queue))
这不是一种编写代码的好方法,如果我想换硬币,效率很低。 我使用 API 获取需要计算的硬币。 所以我需要手动更新 100+ 行调用每个硬币的函数并更新一些其他文件以确保一切运行顺利。
什么是解决这个问题的好方法?
我见过 asyncio 的线程/多线程/更复杂的用法,但我不知道如何将它应用到这个问题上。
我们将不胜感激。
【问题讨论】:
标签: python multithreading websocket python-asyncio python-multithreading