<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: JimRh</title>
    <description>The latest articles on DEV Community by JimRh (@jimrh).</description>
    <link>https://dev.to/jimrh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1024367%2F5a66f33e-95b3-42cd-a0fd-cf98b8c1be27.png</url>
      <title>DEV Community: JimRh</title>
      <link>https://dev.to/jimrh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jimrh"/>
    <language>en</language>
    <item>
      <title>How to handle threads with multiple gunicorn workers to get consistent result</title>
      <dc:creator>JimRh</dc:creator>
      <pubDate>Fri, 10 Feb 2023 15:24:28 +0000</pubDate>
      <link>https://dev.to/jimrh/how-to-handle-threads-with-multiple-gunicorn-workers-to-get-consistent-result-4356</link>
      <guid>https://dev.to/jimrh/how-to-handle-threads-with-multiple-gunicorn-workers-to-get-consistent-result-4356</guid>
      <description>&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>career</category>
    </item>
  </channel>
</rss>
