我想问一下,现在我正在做python3 http web服务器。然而,当它停留在“while-true”时,它在“if-condition”上有问题。当我想使用其他“if条件”时,程序停留在“while-true”,不能继续执行其他程序。
from http.server import BaseHTTPRequestHandler, HTTPServer
import subprocess
Request = None
class RequestHandler_httpd(BaseHTTPRequestHandler):
def do_GET(self):
global Request
messagetosend = bytes('Hello Worldddd!',"utf")
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', len(messagetosend))
self.end_headers()
self.wfile.write(messagetosend)
Request = self.requestline
Request = Request[5 : int(len(Request)-9)]
print(Request)
if Request == 'onAuto':
def always_run():
subprocess.run("python3 satu.py ;", shell=True)
subprocess.run("python3 dua.py ;", shell=True)
while True:
always_run() #the program stuck here and other if cannot be used
if Request == 'onFM':
subprocess.run("python3 satu.py ;", shell=True)
if Request == 'onQR':
subprocess.run("python3 dua.py ;", shell=True)
if Request == 'offSYS':
subprocess.run("python3 OFF_SYSTEM.py ;", shell=True)
return
server_address_httpd = ('X.X.X.X',8080) #my private address
httpd = HTTPServer(server_address_httpd, RequestHandler_httpd)
print('Starting Server')
httpd.serve_forever()发布于 2021-09-01 19:21:04
正如JonSG评论的那样。你的
while True:
always_run()正在阻止代码的进一步执行。所以你必须在一个单独的线程中运行它:
import threading
class AlwaysThread(threading.Thread):
def __init__(self):
super(AlwaysThread, self).__init__()
self.stopThread = False
def run(self):
self.stopThread = False
while not self.stopThread:
always_run()
# where you previously have done the endless loop
t = AlwaysThread()
t.start()
# stop it with t.stopThread = True我还会使用switch语句,而不是if cascade。
https://stackoverflow.com/questions/69017296
复制相似问题