0

Context: For the Raspberry Pi i am developing some home automation tools. At one side i have my main application that reads a CSV file, that consists of date+time entries with a GPIO port number and a duration it needs to send a signal to that port. My main app reads this CSV, creates a small list of entries of this and then basically checks every 60 seconds if there is any job to do. So far so good, this works like a charm.

Now on the other half, i am trying to run a Flask webservice so i can directly interact with this schedule, overwrite, push to refresh the csv, and so on. Later on (future music) i am thinking of making some android app that has a nice GUI that talks with this webservice.

But i keep struggling to start the webservice and then kick off the main app (read csv; execute loop)

some code snipit:

import threading
from flask import Flask, render_template, request
from dwe_homeautomation_app import runMainWorker
        app = Flask(__name__)
        
        # Some routing samples
        @app.route('/app/breakLoop')
        def breakLoop():
            m_worker.breakLoop = True # set global var to exit the 60 sec loop
            return "break!"
        
        if __name__ == '__main__':
            # TODO: how to run this parallel ?
            t1 = threading.Thread(target=app.run(debug=True, use_reloader=False, port=5000, host='0.0.0.0')) # Flask webserver
            t2 = threading.Thread(target=runMainWorker()) # The main app that reads the csv and executes the 60 sec loop
            t1.start()
            t2.start()

As i was reading some topics trough google and stack overflow, but i couldn't really figure out how to get this working in my code; i saw some advice about multi threading, though the info and advice doesn't seem to be very in sync with eachother.

For some reason t1 (the webservice) starts, but t2 doesn't start at all.

Im relative new to Python, so i might be missing the obvious here. Any advice, pointing me in the right direction, or pointing me my mistake in the code sample is much appreciated.

Dante1986
  • 58,291
  • 13
  • 39
  • 54
  • Does this answer your question? [Background Worker with Flask](https://stackoverflow.com/questions/11256002/background-worker-with-flask) – Ken Kinder Jun 22 '20 at 18:31

2 Answers2

0

Try that:

from flask import Flask, render_template, request
from threading import Thread
app = Flask(__name__)

    # Some routing samples
@app.route('/app/breakLoop')
def breakLoop():
    m_worker.breakLoop = True # set global var to exit the 60 sec loop
    return "break!"

def runApp():
    app.run(debug=True, use_reloader=False, port=5000, host='0.0.0.0')

if __name__ == '__main__':
    # TODO: how to run this parallel ?
    Thread(target = runApp).start()
    Thread(target = runMainWorker).start()
JaniniRami
  • 493
  • 3
  • 11
  • same result as my previous sample; runApp works, but then the runMainWorker doesnt seem to do anything – Dante1986 Jun 22 '20 at 18:05
  • Can you share where is the runMainWorker function is placed in your code? – JaniniRami Jun 22 '20 at 18:07
  • runMainWorker i have put as a definition in another .py file that is in the same folder. if i run it separately it works just fine. Or did you mean you wanted some code sample from that as well ? – Dante1986 Jun 22 '20 at 18:10
  • Make sure you imported the function from your other py file because threading doesent show any errors if the function isn't defined, and if you made sure that u imported it, can you share a sample of the code being run in the runMainWorker. – JaniniRami Jun 22 '20 at 18:13
  • yea forgot to copy it into the code snipit; `from dwe_homeautomation_app import runMainWorker` – Dante1986 Jun 22 '20 at 18:24
0

Check the threading.Thread docs: https://docs.python.org/3/library/threading.html#thread-objects You have to pass the target without brackets and args/kwargs as defined in the docs.

potato_cannon
  • 283
  • 2
  • 9