Pyqt Flask1.界面和Flask互不影响 2.启动Flask多线程就像SpringBoot一样

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

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值