Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have Python code to continue executing after I call Flask app.run?

I have just started with Python, although I have been programming in other languages over the past 30 years. I wanted to keep my first application simple, so I started out with a little home automation project hosted on a Raspberry Pi.

I got my code to work fine (controlling a valve, reading a flow sensor and showing some data on a display), but when I wanted to add some web interactivity it came to a sudden halt. Most articles I have found on the subject suggest to use the Flask framework to compose dynamic web pages. I have tried, and understood, the basics of Flask, but I just can't get around the issue that Flask is blocking once I call the "app.run" function. The rest of my python code waits for Flask to return, which never happens. I.e. no more water flow measurement, valve motor steering or display updating.

So, my basic question would be: What tool should I use in order to serve a simple dynamic web page (with very low load, like 1 request / week), in parallel to my applications main tasks (GPIO/Pulse counting)? All this in the resource constrained environment of a Raspberry Pi (3). If you still suggest Flask (because it seems very close to target), how should I arrange my code to keep handling the real-world events, such as mentioned above?

(This last part might be tough answering without seeing the actual code, but maybe it's possible answering it in a "generic" way? Or pointing to existing examples that I might have missed while searching.)

like image 650
Eriond Avatar asked Oct 29 '17 21:10

Eriond


People also ask

How do I keep the Flask app running?

To keep running the application (so that you may close your laptop and have some fun, while the application keeps running), we will use the powerful linux command: nohup (no hangup). We can see after the nohup command the 'process id' : 21108 of the running process.

How does Python integrate with Flask?

You use the python command line interface with the option -c to execute Python code. Next you import the flask package with import flask; then print the Flask version, which is provided via the flask. __version__ variable. You've created the project folder, a virtual environment, and installed Flask.

Can you use both Flask and Django together?

Here is a simple way: You write the two applications, one in Flask and one in Django. Let's assume you solve all the problems you will have when trying to share database or other resources and now you have the two applications running, each with its own web server and each listening for requests on a different port.


1 Answers

You're on the right track with multithreading. If your monitoring code runs in a loop, you could define a function like

def monitoring_loop():
    while True:
        # do the monitoring

Then, before you call app.run(), start a thread that runs that function:

import threading
from wherever import monitoring_loop

monitoring_thread = threading.Thread(target = monitoring_loop)
monitoring_thread.start()

# app.run() and whatever else you want to do

Don't join the thread - you want it to keep running in parallel to your Flask app. If you joined it, it would block the main execution thread until it finished, which would be never, since it's running a while True loop.

To communicate between the monitoring thread and the rest of the program, you could use a queue to pass messages in a thread-safe way between them.

like image 194
Josh Karpel Avatar answered Sep 21 '22 01:09

Josh Karpel