DEV Community

JimRh
JimRh

Posted on

How to handle threads with multiple gunicorn workers to get consistent result

Hi I have a flask app which needs to run a background task at certain intervals,I have tried to achieve this by using threading.The issue is my server needs to handle lots of request coming to it and so I assigned multiple workers in gunicorn to handle it but when I do that my background task gives inconsistent result,for a certain time interval it should be executed only once but sometimes it gets executed multiple times instead.If i reduce the worker count to 1 in gunicorn the background tasks behave as expected.

import time
import threading


def writemotiondata():

    deviceid='abcdd33656'
    while True:
            response = openapi.get("/v1.0/devices/{}".format(deviceid))

            result = response['result']
            status = result['status']
            value = status[0]['value']


            print(value)
            if (value == True):
                tempdata = motiondata.find()
                previousdata = [{item: key[item] for item in key if item != '_id'} for key in tempdata]

                if len(previousdata) == 0:

                    data = {"sensorname": "pir",
                            "numberofpeople": 1}
                    motiondata.insert_one(data).inserted_id

                else:
                    count = previousdata[0]['numberofpeople']
                    count += 1
                    filter = {"sensorname": "pir"}
                    updateddata = {"$set": {"numberofpeople": count}}
                    motiondata.update_one(filter, updateddata)

                time.sleep(40)


            else:

                time.sleep(10)




if __name__ == '__main__':
    t1 = threading.Thread(target=writemotiondata)
    t1.start()
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

Can anyone please help me how to get consistent result for this background task using multiple workers in gunicorn. I have tried using thread lock but it didn't work.Thanks in advance.

Top comments (0)