python 同步代码 改为异步

发布于 2020-12-30 11:54:51

import numpy as np
import tushare as ts
import aiohttp
import asyncio
import time

codes = ['787819', '787317', '787317', '600320', '600191', '600191', '600967', '600765', '600238', '603650']

cvs_30 = []
start = time.time()

async def get_index(code):

all_data = ts.get_hist_data(code, start='2020-05-12', end='2020-12-28')
if all_data is None:

    cv_30 = 0
    # 加入相对应的列表
    cvs_30.append(cv_30)


else:
    data = all_data['close']
    cv_30 = np.std(data.head(30)) / np.mean(data.head(30))

    # 加入相对应的列表
    cvs_30.append(cv_30 * 1000)

tasks = [asyncio.ensure_future(get_index(code)) for code in codes]

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks))
end = time.time()

print(end - start)

查看更多

关注者
0
被浏览
75
0 个回答
暂无答案,快来添加答案吧

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览