Error after bsm.stop()
sniffski opened this issue · 3 comments
Hello,
At the end of my code I've added:
bsm.stop()
sleep (5)
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 ...
Please help me solve it...
Can you post more detail? maybe a sample of your code so I can try to reproduce it?
The websocket example also uses bsm.stop(), there has not been any other reports of this issue and it works fine on my end.
#!/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)
bsm.stop()
sleep(5)
pass
I was able to replicate the issue. It looks like an asyncio task is not awaited before the asyncio loop is closed.
This is something you will need to raise a new issue with on the python-binance github repository. It looks like a fairly major update was pushed out on Sept 8 (1.0.13). I tested the above code after downgrading to 1.0.10 and it worked fine so a more recent code change on their end seems to have triggered it.