'Returing result of synchonous function inside async function in python
I'm newbie in the world of python and have been trying to solve the following for last 3 days on my own. I read many articles online none of them address my problem or I seem to been missing something so I decided to post my question here.
Purpose:-
I'm trying to connect realtime data from my broker to a charting library via 'socketio python' websocket which I host locally and runs on an ASGI server. Broker has provided following two sync functions which I've wrapped inside an async fuction SubAdd
but couldn't get an output from
@sio.event
async def SubAdd(sid,data): #event that handles subscribe requests from library for realtime data
def socket(access_token): #This funtion gets the data from broker
data_type = "symbolData"
symbol =["NSE:NIFTYBANK-INDEX"]
fs = ws.FyersSocket(access_token=access_token,run_background=False,log_path="/home/log/")
fs.websocket_data = custom_message
fs.subscribe(symbol=symbol,data_type=data_type)
fs.keep_running()
def custom_message(msg):# Function that returns fetched data
print (f"Custom:{msg}")
socket(access_token)
A thing that confused me the most is at the line 4 of socket
i.e.fs.websocket_data = custom_message
. Normally, if we write A=B then left side of assignment operator gets assigned the value of right side i.e. A is getting value from B But something else is happening here I dont know what?
Thing I've tried:
making both socket and custome_message an async fuction and then yielding sio.emit from inside async
custom_message
making both socket and custome_message an async fuction and then awiting/yielding to the append msg to another list and then using
async for
on that list and then await sio.emit
Both of above gave an error RuntimeWarning: coroutine 'SubAdd.<locals>.socket' was never awaited
which clearly means I'm heading in the wrong direction.
So my question is how do I wrap these two sync functions inside an async function and get an awaitable output from custom_message
. If you could directly answer the question its well and good but even if you point me to the a link to a resource or even a keyword to search, which you think would answer my question is greatly appreciated. Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|