Python 如何使用asyncio拆分流数据及其工作机制
使用以下代码来流式传输数据并执行某些计算 代码是异步的,以便函数在某些中断后一个接一个地运行 异步def on_ticks提供流数据。 _connect上的异步def连接到web套接字。 async def works执行流数据的计算 虽然代码似乎正确,但我收到了警告 运行时警告:从未等待协同路由“on_connect” self.on_connect(self,response RuntimeWarning:启用tracemalloc以获取对象分配回溯 我哪里错了 ''' df=pd.DataFrame(数据=None) '''Python 如何使用asyncio拆分流数据及其工作机制,python,Python,使用以下代码来流式传输数据并执行某些计算 代码是异步的,以便函数在某些中断后一个接一个地运行 异步def on_ticks提供流数据。 _connect上的异步def连接到web套接字。 async def works执行流数据的计算 虽然代码似乎正确,但我收到了警告 运行时警告:从未等待协同路由“on_connect” self.on_connect(self,response RuntimeWarning:启用tracemalloc以获取对象分配回溯 我哪里错了 ''' df=pd.Data
async def on_ticks(ws, ticks):
global df
for sc in ticks:
token=sc['instrument_token']
name=trd_portfolio[token]['name']
ltp=sc['last_price']
df1=pd.DataFrame([name,ltp]).T
df1.columns=['name','ltp']
df=df.append(df1,ignore_index=True)
await asyncio.sleep(5)
async def work():
global df
df['change']=df.groupby('name')['ltp'].pct_change()
g = df.groupby('name')['change']
g = g.agg(
pos_count=lambda s: s.gt(0).sum(),
neg_count=lambda s: s.lt(0).sum(),
net_count=lambda s: s.gt(0).sum()- s.lt(0).sum()).astype(int)
g=g.sort_values(by=['net_count'])
print(g)
await asyncio.sleep(1)
async def on_connect(ws, response):
ws.subscribe(inst_token)
ws.set_mode(ws.MODE_LTP,inst_token)
await asyncio.sleep(1)
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect()
async def main():
await asyncio.gather(
on_connect(),
on_ticks(),
work()
)
asyncio.run(main())