Flask 运行时无法在 python 中打印到控制台

问题描述 投票:0回答:1

所以我正在尝试用 Python 制作一个任务管理器网站,它有一个管理控制台和一个前端网站。最初,一切工作正常,直到我添加了 Flask 网页的代码(如 WebManager_Main.py 所示),然后控制台打印(如 CommandLine_Output 所示)停止工作。奇怪的是,它们在两个不同的线程中运行(如 main.py 所示),所以我不知道这是怎么发生的!

主要.py

# Startup and import
# import libraries
import threading
import time
# import files
import CommandLine_Handler as clh
import DataBaseHandler_Main as DBHM
import WebManager_main as WBM
import Error_Handler as eh
import Mailbox as mb

# Startup
mb.run_Main = True
mb.run_Interface = True
mb.run_Database = True
mb.run_WebApp = True
mb.loop_Counter = 0

cmdInterface = None
dataBaseCreate = None
WebAppManager = None

# Run Loop
while mb.run_Main == True:
  if mb.run_Interface == True:
    try:
      if cmdInterface == None:
        cmdInterface = threading.Thread(target = clh.interface)
        cmdInterface.daemon = True
        cmdInterface.start()
      else:
        if not cmdInterface.is_alive():
          cmdInterface = threading.Thread(target = clh.interface)
          cmdInterface.daemon = True
          cmdInterface.start()
    except:
      eh.error("m-rl1")
      
  if mb.run_Database == True:
    try:
      if dataBaseCreate == None:
        dataBaseCreate = threading.Thread(target = DBHM.creator)
        dataBaseCreate.daemon = True
        dataBaseCreate.start()
      else:
        if not dataBaseCreate.is_alive():
          dataBaseCreate = threading.Thread(target = DBHM.creator)
          dataBaseCreate.daemon = True
          dataBaseCreate.start()
    except:
      eh.error("m-rl2")

  if mb.run_WebApp == True:
    try:
      if WebAppManager == None:
        WebAppManager = threading.Thread(target = DBHM.creator)
        WebAppManager.daemon = True
        WebAppManager.start()
      else:
        if not WebAppManager.is_alive():
          WebAppManager = threading.Thread(target = WBM)
          WebAppManager.daemon = True
          WebAppManager.start()
    except:
      eh.error("m-rl3")

        
  mb.loop_Counter += 1
  time.sleep(0.5)

命令行_输出.py

import os
import sys
import pwinput as pIN
import logging
def output(input):
  print(input, flush=True)
  # logger = logging.getLogger('werkzeug')
  # logger.info(input)

def uInput(prompt):
   userInput = input(prompt)
   return userInput
def passInput(prompt):
  #userInput = pIN.pwinput(prompt=prompt, mask = '*')
  userInput = input(prompt)
  return userInput

def clear():
  os.system("cls" if os.name == "nt" else "clear")
  #os.system("cls")

WebManager_Main.py

from flask import Flask, render_template
#this is the library responsible for the hosting of the Web app
webApp = Flask(__name__)

@webApp.route("/")

def index():
  return render_template("index.html")

if __name__ == "WebManager_main":
  webApp.run(port=8080, debug = True)

我在网上浏览了一些有类似问题的人的帖子以及他们提出的建议。我尝试过刷新缓冲区、记录日志以及尝试禁用 Flask 的打印。 但我似乎无法再从该线程获得控制台打印了。

我非常感谢任何帮助!

python python-3.x multithreading flask printing
1个回答
0
投票

Python Web 应用程序必须由实现 WSGI 协议的软件独立运行。

请参阅 Flask 文档 了解如何启动您的 Web 应用程序。您不能像在代码中那样将其作为线程运行。

© www.soinside.com 2019 - 2024. All rights reserved.