I am trying to set a timeout so my database will be uploaded with record every 1 second. i have tried solution from python-binance documentation, but it seem like it's not working.
Right now i am receiving too much data f.e. 20 records in 1 second and i would like it to get 1 record for 1 second.
python-binance doc: https://python-binance.readthedocs.io/en/latest/websockets.html#threadedwebsocketmanager-websocket-usage
Function with timeout:
bm = BinanceSocketManager(client, user_timeout=1000)
My code:
import pandas as pd
import sqlalchemy
import asyncio
from binance import AsyncClient, BinanceSocketManager
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:, ['s', 'E', 'p']]
df.columns = ['symbol', 'Time', 'Price']
df.Price = df.Price.astype(float)
df.Time = pd.to_datetime(df.Time, unit='ms')
return df
async def main():
client = await AsyncClient.create()
bm = BinanceSocketManager(client, user_timeout=1000)
# start any sockets here, i.e a trade socket
ts = bm.trade_socket('BTCUSDT')
# then start receiving messages
async with ts as tscm:
while True:
res = await tscm.recv()
frame = createframe(res)
print(frame)
frame.to_sql('BTCUSDT', 'sqlite:///stream.db', if_exists='append', index=False)
await client.close_connection()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())