# coding=utf-8
# -*- coding: utf-8 -*-
import sys
import threading
from PyQt4.QtGui import QMainWindow, QApplication
from astropy.utils.compat.futures import ThreadPoolExecutor
from flask import Flask, request
from flask.json import jsonify
import mainwindow
import json
import time
app = Flask(__name__)
# 创建线程池执行器
executor = ThreadPoolExecutor(10) # 参数设置线程池大小
@app.route('/get_01', methods=['GET', 'POST'])
def get_01():
if request.method == 'POST':
# 获取请求体内容并解析为JSON格式
print('result::' + request.data)
data = json.loads(request.data)
# if not data or 'Body' not in data or 'Head' not in data:
# return jsonify({"error": "Invalid request body"}), 400
body_content = data['Body']
head_value = data['Head']
code = body_content.get('Code')
reason = body_content.get('Reason')
# 对接收到的数据进行处理...
print("Code: {}, Reason: {}, Head: {}".format(code, reason, head_value))
# 示例返回响应
response_data = {
"Status": "Received",
"Body": {"Code": code, "Reason": reason},
"Head": head_value
}
time.sleep(6)
return jsonify(response_data), 200
else:
# 如果是GET请求,可以渲染一个包含提交表单的HTML页面
return 'GET'
@app.route('/get_02', methods=['GET', 'POST'])
def get_02():
if request.method == 'POST':
# 获取请求体内容并解析为JSON格式
print('result::' + request.data)
data = json.loads(request.data)
# if not data or 'Body' not in data or 'Head' not in data:
# return jsonify({"error": "Invalid request body"}), 400
body_content = data['Body']
head_value = data['Head']
code = body_content.get('Code')
reason = body_content.get('Reason')
# 对接收到的数据进行处理...
print("Code: {}, Reason: {}, Head: {}".format(code, reason, head_value))
# 示例返回响应
response_data = {
"Status": "Received",
"Body": {"Code": code, "Reason": reason},
"Head": head_value
}
time.sleep(6)
return jsonify(response_data), 200
else:
# 如果是GET请求,可以渲染一个包含提交表单的HTML页面
return 'GET'
@app.route('/get_03', methods=['GET', 'POST'])
def get_03():
if request.method == 'POST':
# 获取请求体内容并解析为JSON格式
print('result::' + request.data)
data = json.loads(request.data)
# if not data or 'Body' not in data or 'Head' not in data:
# return jsonify({"error": "Invalid request body"}), 400
body_content = data['Body']
head_value = data['Head']
code = body_content.get('Code')
reason = body_content.get('Reason')
# 对接收到的数据进行处理...
print("Code: {}, Reason: {}, Head: {}".format(code, reason, head_value))
# 示例返回响应
response_data = {
"Status": "Received",
"Body": {"Code": code, "Reason": reason},
"Head": head_value
}
return jsonify(response_data), 200
else:
# 如果是GET请求,可以渲染一个包含提交表单的HTML页面
return 'GET'
@app.route('/')
def home():
return "Hello, Flask is running!"
# 耗时任务
def long_task():
print('long task start')
time.sleep(6)
print("Task is done!")
def run_flask_server():
# 在子进程中启动 Flask 服务器
try:
app.run(debug=False, host='192.168.1.97', port=5000,threaded=True)
except Exception as e:
print('error::' + str(e))
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.ui = mainwindow.Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == '__main__':
# 创建一个线程
thread_process = threading.Thread(name='thread_process', target=run_flask_server)
thread_process.start()
# 实例化MyWindow类
app_qt = QApplication(sys.argv)
# 实例化MyWindow类
myWindow = MyWindow()
# 显示
myWindow.show()
# 执行程序
sys.exit(app_qt.exec_())
Pyqt Flask1.界面和Flask互不影响 2.启动Flask多线程就像SpringBoot一样
最新推荐文章于 2024-06-04 14:22:08 发布