在Flask运行时无法在控制台打印Python信息

1 投票
1 回答
52 浏览
提问于 2025-04-13 12:40

我正在用Python制作一个任务管理网站,里面有一个管理控制台和一个前端网站。最开始一切都运行得很好,直到我添加了Flask网页的代码(在WebManager_Main.py中显示),然后控制台的输出(在CommandLine_Output中显示)就停止了。奇怪的是,这两个部分是在两个不同的线程中运行的(在main.py中显示),所以我完全不知道这是怎么发生的!

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

CommandLine_Output.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的打印功能。但我似乎再也无法从那个线程中获取控制台输出了。

如果有人能提供任何帮助,我将非常感激!

1 个回答

0

一个Python网页应用需要通过一个实现了WSGI协议的软件独立运行。

可以查看Flask的文档,了解如何启动你的网页应用。你不能像在代码中那样把它当作一个线程来运行。

撰写回答