Hey I am runnig my python code under Linux on a Raspberry Pi and it consists of a main.py file that opens threads to do various tasks
For more insights: my filestructure looks like this: main.py modules ----controller.py ----server.py ----reader.py ----logger.py ----dbhandling.py
-the controller has a loop in witch it looks for new entrys in the database and reacts to them -the server is just a flask application -the logger has a cons_log() function in which I can output data in the same format to the terminal. It uses termcolor, which doesnt work with the coderunner output, thats why there is this weird asci shown sometimes -the dbhandler is just there to read and write the db conveiniently -the reader reads out a rfid reader via the raspberrypi gpio. It listens all the time for new rfid tags.
The whole main.py looks like this:
import time
import threading
from datetime import datetime
from modules.server import Serv
from modules.logger import cons_log
from modules.reader import ReaderInteraction as ReaderI
from modules.controller import ControllerLoop
from modules.dbhandling import DBinteractions as Db
while True:
try:
cons_log("MAIN", "Starting ControllerLoop...", "yellow", "0")
controller_threat = threading.Thread(target=ControllerLoop().loop)
controller_threat.name = "Controller Threat"
controller_threat.start()
time.sleep(1)
cons_log("MAIN", "Successfully started ControllerLoop", "green", "202")
controller_threat.join()
break
except Exception as e:
cons_log(
"MAIN", "Could not start ControllerLoop, Error: \n" + str(e), "red", "404"
)
time.sleep(5)
while True:
try:
cons_log("MAIN", "Starting Server...", "yellow", "0")
server_threat = threading.Thread(target=Serv)
server_threat.name = "Server Threat"
server_threat.start()
time.sleep(1)
cons_log("MAIN", "Successfully started Server", "green", "202")
server_threat.join()
break
except Exception as e:
cons_log("MAIN", "Could not start Server, Error: \n" + str(e), "red", "404")
time.sleep(5)
while True:
try:
cons_log("MAIN", "Starting Reader...", "yellow", "0")
reader_threat = threading.Thread(target=ReaderI().runreader)
reader_threat.name = "Reader Threat"
reader_threat.start()
time.sleep(1)
cons_log("MAIN", "Successfully started Reader", "green", "202")
reader_threat.join()
break
except Exception as e:
cons_log("MAIN", "Could not start Reader, Error: \n" + str(e), "red", "404")
time.sleep(5)
The controller looks like this:
# module used to control the hardware and electric systems via constant database readouts
import RPi.GPIO as GPIO
import time
class ControllerLoop:
def __init__(self):
None
def loop(self):
while True:
time.sleep(0.3)
# check for new controlls to do based on entrys in database
The server looks like this:
from flask import Flask, flash
def Serv():
app = Flask(__name__, template_folder="../templates")
# all the routs in here
app.secret_key = "tpsecret..."
app.run(debug=False, port=8000, host="0.0.0.0")
The reader looks like this:
import time
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
class ReaderInteraction:
def __init__(self):
self.reader = SimpleMFRC522()
def runreader(self):
while True:
try:
id, text = self.reader.read()
print(id)
time.sleep(1)
finally:
GPIO.cleanup()
The logger looks like this:
from datetime import datetime
from termcolor import colored
from time import time
from flask import Flask, redirect, flash, url_for
def cons_log(process, message, color, code):
flask_ip = "127.0.0.1"
time = datetime.today()
print(
flask_ip
+ " - - ["
+ time.today().strftime("%d/%m/%Y")
+ " "
+ time.today().strftime("%H:%M:%S")
+ "] "
+ '"'
+ colored(process + " " + message + '"', color)
+ " "
+ code
+ "-"
)
def flask_log(process, message, color, code, flashmsg, flashtype, redir):
# def to communicate with user
flask_ip = "127.0.0.1"
time = datetime.today()
print(
flask_ip
+ " - - ["
+ time.today().strftime("%d/%m/%Y")
+ " "
+ time.today().strftime("%H:%M:%S")
+ "] "
+ '"'
+ colored(process + " " + message + '"', color)
+ " "
+ code
+ "-"
)
flash(flashmsg, flashtype)
return redirect(url_for(redir))
When using .join() at the end of the main.py for all threads the output stops after the first .join() call.
Question: If I use the .join() on the controllerloop does the main program then waits with the execution of the next code untill the controllerloop is finished? Because if so the controllerloop is running endless and the main would never continue right?