Announcing Dash Bio 1.0.0 ๐ŸŽ‰ : a one-stop-shop for bioinformatics and drug development visualizations.

Multithreading problem: Double calling one of the two parallel tasks

Hi, I am quite new to Dash and python.

I am trying to set up an application on a raspberry pi. I want to run a flask server with dash application to control my sensors connected to rpi and visualize the collected data. For this purpose, I have created one function (make_measurements) responsible for reading the data and storing it in the database. In this task I also manage a state machine class to control how often the data should be read etc. The state machine reads a request from the DB sent by a web client.

The problem is that when I run my application it looks like I am running the same script twice. I donโ€™t know why I have 4 threads. Logs below:

My code:
app.py

import time, threading

from flask import Flask

from dashApp.webapp import create_app

from statemachine import Guard, Idle, Calibration, State

from database import *

app = Flask(__name__, instance_relative_config=False)

app, db = create_app(app)

def make_measurement():

    comp = Guard(State, db)

    comp.state.print_state()

    comp.state.initialization()

    # Initialization process

    if comp.state.isInitialized():

        comp.set_init_status(COMPLETED)

        if comp.isCalibrated():

            comp.change_state(Idle)

   

        else:

            comp.change_state(Calibration)

            comp.state.calibration()

            comp.set_calib_status(COMPLETED)

            if comp.isCalibrated() == False:

                raise Exception("Problem with calibration")

           

            comp.change_state(Idle)

    else:

        raise Exception("Problem with initialization")

    comp.state.print_state()

    while(True):

        print("task started")

        print("the number of Thread objects currently alive: ", threading.active_count())

        print("current thread: ", threading.current_thread())

        # comp.state.print_state()

        comp.check()

        time.sleep(5)

        comp.measure_temperature()

        #adc_measurement()

       

        print("task completed")

t2 = threading.Thread(target=make_measurement)

t2.start()

if __name__ == '__main__':

    db.create_all()

    app.run_server(host='192.168.1.106', port=8080,debug=True)

    t2.join()

    # t1 = threading.Thread(target=app.run_server)

    # t1.start()

webapp.py

import dash
from flask_sqlalchemy import SQLAlchemy

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

def create_app(app):
    # inject Dash
    app = dash.Dash(
    __name__,
    server=app,
    url_base_pathname='/dashboard/',
    suppress_callback_exceptions=True,
    )

    app.server.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db/test.db"
    app.server.config['SECRET_KEY'] = 'asd asd asd'

    from dashApp.layout import layout_main
    app.layout = layout_main

    
    from dashApp.callbacks import register_callbacks
    register_callbacks(app)

    db = SQLAlchemy(app.server)

    return app, db

definition of

   def measure_temperature(self):
      print("Measure temperature...")
      tmpSensor = DS1820()
      read_temp=tmpSensor.read_temp()
      
      self.db.ptr_to_database.session.add(Temperature(obj_temp=read_temp, sys_temp=(read_temp), time_of_measurement=datetime.datetime.now()))
      self.db.ptr_to_database.session.commit()

and definition of class DS1829

class DS1820():

    def read_temp(self):
        try:
            sensor = W1ThermSensor()
            temp = sensor.get_temperature()
            print("read temp function:")
            print(temp)
            return temp
        except:
            sum, in_min_temp, in_max_temp =0, 0, 100

            for x in range(5):
                sum += round(random.uniform(in_min_temp, in_max_temp), 1)

            return int(sum/5)