RuntimeWarning: coroutine 'DhanFeed.connect' was never awaited

I referred following code from this link: https://madefortrade.in/t/marketfeed-api-sample-python-code-error/22229/10

import nest_asyncio
nest_asyncio.apply()
import credentials
from dhanhq import marketfeed
import asyncio

instruments = [(1, "1333"),(0,"13")]
subscription_code = marketfeed.Ticker

# Usage Example
async def on_connect(instance):
    print("Connected to websocket")

async def on_message(instance, message):
    print("Received:", message)

print("Subscription code :", subscription_code)
feed = marketfeed.DhanFeed(credentials.client_id,
    credentials.access_token, instruments, subscription_code,
                    on_connect=on_connect, on_message=on_message)

feed.run_forever()

asyncio.create_task(feed.run_forever())

Above code giving error:

RuntimeWarning: coroutine 'DhanFeed.connect' was never awaited
  self.connect()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Connected to websocket
Connected to websocket
from dhanhq import marketfeed
import credentials

# Structure for subscribing is ("exchange_segment","security_id")
# Maximum 100 instruments can be subscribed, then use 'subscribe_symbols' function 
instruments = [(0,"13")]

# Type of data subscription
subscription_code = marketfeed.Quote
# Ticker - Ticker Data
# Quote - Quote Data
# Depth - Market Depth

async def on_connect(instance):
    print("Connected to websocket")

async def on_message(instance, message):
    print("Received:", message)
    # Received: {'type': 'Quote Data', 'exchange_segment': 0, 'security_id': 13, 'LTP': '22331.25', 'LTQ': 0, 'LTT': '14:56:38', 'avg_price': '0.00', 'volume': 0, 'total_sell_quantity': 0, 'total_buy_quantity': 0, 'open': '22048.30', 'close': '21982.80', 'high': '22348.40', 'low': '22047.75'}

print("Subscription code :", subscription_code)

feed = marketfeed.DhanFeed(credentials.client_id,
    credentials.access_token,
    instruments,
    subscription_code,
    on_connect=on_connect,
    on_message=on_message)

feed.run_forever()

This code was working few days ago. But this code also gives same error:

RuntimeWarning: coroutine 'DhanFeed.connect' was never awaited
  self.connect()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Connected to websocket
Connected to websocket

FYI: I have access to Data APIs from Dhan until 04 Apr 2024.
Request you guys to help me on this critical issue.

Hello @Vaibhav722

This is not an error but only warning, and this would not stop your code from running. If markets are live and you have subscribed to correct security ID, then you will receive data on the sockets as well.

1 Like

How to avoid this warning? Since I want my code to remain clean.

Hello @Akash_Prasad

We are working on the next version of DhanHQ Python Library update, this should be fixed then.