Flask Thread Not Working when Deployed in uWSGI server

Viewed 90

I have done threading in my Flask Application. I have to log my data in a separate table in mysql which has to work asynchronously. So my main function collects all the data and before sending the flask response i am calling the thread. So that the Response is also sent on time and also my thread function runs in background. This method works fine when run in local flask server. But when i deploy this in uWSGI server i need to enable thread in uWSGI. After that when my thread function is called the data in the thread is lost and there no value in my variable.

My Mail flask Function

@app.route('/', methods=['POST'])
def mainfunction():
    Dictionary['Name'] = 'MyName'
    Dictionary['Age'] = 'MyAge'
    Dictionary['Address'] = 'MyAddress'
    t1 = threading.Thread(target=loadinDBUsingThread, args=(Dictionary,))
    t1.start()
return json.dumps(Dictionary)

My Thread Function

def loadinDBUsingThread(Dictionary):
    localVariable0 = Dictionary['Name']
    localVariable1 = Dictionary['Age']
    localVariable2 = Dictionary['Address']
    #Insert these variables to the Database

I got the Keyerror Name is not found in the Dictionary. I don't know how my variables are getting lost. Please help me with this.

0 Answers
Related