Flask app web hook double execution issue!!

I have created a python flask app that receives web hook alerts from trading view and execute them on brokers account. The problem is if stop loss is triggered after entry in trading view it send two stop loss orders at same time and same syntax its because I use partial profit exit. ISSUE is when I receive two alerts at same time due to flask concurrency it execute both orders and I get extra position in my brokers account. here is the below code for better understanding

@app.route('/webhook_pivot', methods=[ 'POST']) def webhook(): # Get the data from the request data = json.loads( data_str = json.dumps(data, sort_keys=True) #print("Incoming webhook data:", data) side = data['side'] qty = int(data['qty'] ) * int(lot)

if side == "BUY" :

    #buy  execution code here.....

elif side == "FULL_EXIT_BUY" and check_symbol_and_position(sym):

    with lock:
        if data_str in cache:

            # If the data is in the cache, it's a duplicate and should be ignored
            print("Double msg ignore")
            return "Duplicate data, ignoring"

            # BUY EXIT order ...but it send this part twice ..!!! the problem

My advice would be to add more logging (print statements) to try to figure out why it's being executed twice

I know why its being executed twice because the incoming msg is coming two times..I problem is how can I ignore the second message ?

You need to preserve that information somewhere, maybe in the database?