准备一台linux系统环境
安装 cx_Freeze
pip install cx_Freeze
准备两个py脚本
1,app应用脚本,需要打包的 app.py
import random
import time
from flask import Flask, jsonify
from concurrent.futures import ThreadPoolExecutor
app = Flask(__name__)
executor = ThreadPoolExecutor()
@app.route('/api')
def api():
# 提交一个任务到线程池
future = executor.submit(process_request)
# 使用Future对象的result()方法获取任务的执行结果
return 'Request dd!'
# result = future.result()
# # 返回结果给客户端
# return jsonify(result)
def process_request():
# 处理请求的逻辑
result = {'message': 'Request processed!'}
num = random.randint(1, 10)
print(f'睡眠:{num}')
time.sleep(num)
print(f'结束睡眠:{num}')
return result
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, threaded=True)
2,准备一个打包脚本setup.py
from cx_Freeze import setup, Executable
setup(name="MyApplication",
version="0.1", description="My Python Application",
executables=[Executable("app.py")]
)
3,在linux系统上运行
python setup.py build