I have a flask up with an endpoint like below
@app.route('/update_tokens', methods=['POST'])
async def update_tokens():
# global variables
data = request.get_json()
auth_token = data['auth_token']
pragma_cache = data['pragma_cache']
print("Auth token updated:", auth_token)
print("Pragma cache updated:", pragma_cache)
with open("credentials.txt", "w") as file1:
file1.write(f"{auth_token}n{pragma_cache}n")
loop = asyncio.get_event_loop()
if user_ws:
await loop.run_in_executor(None, user_ws.close)
headers['authorization'] = auth_token
headers['pragma-cache-local'] = pragma_cache
#updating some other stuff ...
print('USER_ID: ' + user_id)
# Create a new task for updating the display list
#update_task = asyncio.create_task(update_display_list(user_response_as_json))
#await update_task
# Reuse the existing thread pool to create the WebSocket connection
# Start and stop the WebSocket connection within the route
await loop.run_in_executor(ws_executor, functools.partial(create_websocket_connection_wallet, ws_url, loop))
return jsonify({"message": "Credentials updated successfully"}), 200
def create_websocket_connection_wallet(url, loop):
global user_ws
def on_close(ws):
loop.call_soon_threadsafe(loop.stop) # Stop the event loop when the WebSocket closes
user_ws = websocket.WebSocketApp(ws_url, on_message=user_handler, on_close=on_close)
user_ws.run_forever(reconnect=5)
print("WebSocket wallet opened. Subscribing to channels...")
return user_ws
This endpoint works actually and it does everything i want it to do but the endpoint is not returning a success response back to frontend that is my problem.
Any help or guidance would help me a lot
I tried creating multiple threads but it would create infinitly many threads i am stuck with this final approach and couldnt figure out any other way.