To allow proper socket closure before exit, however I'm getting this error on exit:
Task was destroyed but it is pending!
task: <Task pending coro=<ReconnectingWebsocket._read_loop() done, defined at ...
It looks like an asyncio task is not awaited before the asyncio loop is closed.
You can reproduce with:
#!/usr/bin/env python3
import sys
import os
import datetime
import time
from time import sleep
import btalib
btalib.config.set_return_dataframe() # force return of a DataFrame
import math
import numpy as np
import pandas as pd
import json
from binance.client import Client
from binance.enums import *
from binance.exceptions import BinanceAPIException, BinanceOrderException
from binance import ThreadedWebsocketManager
# init
api_key = os.environ.get('binance_api')
api_secret = os.environ.get('binance_secret')
client = Client(api_key, api_secret)
#client.API_URL = 'https://testnet.binance.vision/api'
btc_price = {'error':False}
btc_depth = {'error':False}
def btc_pairs_trade(msg):
''' define how to process incoming WebSocket messages '''
if msg['e'] != 'error':
print(msg)
btc_price['last'] = msg['c']
btc_price['bid'] = msg['b']
btc_price['ask'] = msg['a']
btc_price['error'] = False
else:
btc_price['error'] = True
def btc_depth_message(msg):
''' define how to process incoming WebSocket messages '''
if msg['e'] != 'error':
btc_depth['bid'] = sum(list(map(lambda num: float(num[1]), msg['b'])))
btc_depth['ask'] = sum(list(map(lambda num: float(num[1]), msg['a'])))
print(json.dumps(btc_depth, indent=2))
btc_depth['error'] = False
else:
btc_depth['error'] = True
# init and start the WebSocket
bsm = ThreadedWebsocketManager()
bsm.start()
btc_price_stream = bsm.start_symbol_ticker_socket(symbol='BTCUSDT', callback=btc_pairs_trade)
btc_depth_stream = bsm.start_depth_socket(symbol='BTCUSDT', callback=btc_depth_message)
try:
while True:
pass # Do something
except KeyboardInterrupt:
bsm.stop_socket(btc_price_stream)
bsm.stop_socket(btc_depth_stream)
sleep(5)
bsm.stop()
pass
To allow proper socket closure before exit, however I'm getting this error on exit: Task was destroyed but it is pending! task: <Task pending coro=<ReconnectingWebsocket._read_loop() done, defined at ...
It looks like an asyncio task is not awaited before the asyncio loop is closed.
You can reproduce with: