【发布时间】:2018-04-24 12:14:28
【问题描述】:
我是python编程的新手,我已经编程了大约半年。我决定尝试构建一个功能性交易机器人。在尝试编写这个机器人时,我偶然发现了 asyncio 模块。我真的很想更好地理解这个模块,但是很难找到任何关于 asyncio 的简单教程或文档。
对于我的脚本,我收集每个硬币的数量。这非常有效,但收集所有卷需要很长时间。我想问一下我的脚本是否同步运行,如果是,我该如何解决?我正在使用 API 包装器与 Binance Exchange 进行通信。
import binance
import asyncio
import time
s = time.time()
names = [name for name in binance.ticker_prices()] #Gathering all the coin names
loop = asyncio.get_event_loop()
async def get_volume(name):
async def get_data():
return binance.ticker_24hr(name) #Returns per coin a dict of the data of the last 24hr
data = await get_data()
return (name, data['volume'])
tasks = [asyncio.ensure_future(get_volume(name)) for name in names]
results = loop.run_until_complete(asyncio.gather(*tasks))
print('Total time:', time.time() - s)
【问题讨论】:
标签: python-asyncio