0

嘿,我在 Raspberry Pi 上的 Linux 下运行我的 python 代码,它由一个 main.py 文件组成,该文件打开线程以执行各种任务

有关更多见解:我的文件结构如下所示: main.py modules ----controller.py ----server.py ----reader.py ----logger.py ----dbhandling.py

- 控制器有一个循环,它在数据库中查找新条目并对其做出反应 - 服务器只是一个烧瓶应用程序 - 记录器有一个 cons_log() 函数,我可以在其中以相同格式将数据输出到终端. 它使用 termcolor,它不适用于 coderunner 输出,这就是为什么有时会显示这个奇怪的 asci - dbhandler 只是为了方便地读取和写入 db - 阅读器通过 raspberrypi gpio 读出 rfid 阅读器。它一直在监听新的 rfid 标签。

整个 main.py 看起来像这样:

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)



控制器如下所示:

# 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

服务器看起来像这样:

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")

阅读器看起来像这样:

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()

记录器如下所示:

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))

在所有线程的 main.py 末尾使用 .join() 时,输出在第一次 .join() 调用后停止。

问题:如果我在控制器循环上使用 .join() ,主程序是否会等待下一个代码的执行,直到控制器循环完成?因为如果是这样,controllerloop 将无休止地运行,而 main 将永远不会继续,对吗?

4

0 回答 0