Reputation: 35
When using the websocket-client library, in python, is it possible for the on_message function to be a coroutine ?
I have the following code below:
async def on_message(ws, message):
await get_balance() # runs an aiohttp get request
ws = websocket.WebSocketApp(data_feed,on_message=on_message)
ws.run_forever()
This gives me the error
RuntimeWarning: coroutine 'on_message' was never awaited
callback(self, *args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Am I missing something?
Thanks in advance
Upvotes: 2
Views: 1031
Reputation: 6328
The websocket-client
is kinda outdated. You would want to update python-binance library version to at least above 1.0.12 and use the AsyncClient
built in the library to do the asynchronous stuff.
from binance import AsyncClient, BinanceSocketManager
async def on_message(res):
# strategy
async def kline_listener(client):
bm = BinanceSocketManager(client)
async with bm.kline_socket(symbol='ETHBTC') as stream:
while True:
res = await stream.recv()
print(res)
# do as you'd do in on_message() or just call on_message()
await on_message(res)
async def main():
client = await AsyncClient.create(api_key, secret, tld)
await kline_listener(client)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Reference:
Upvotes: 2