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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs