准备一台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
等待程序执行完成,会在当前目录生成build文件夹,依次进入,即可 发现一个linux可执行文件,直接执行即可
可以将默认生成的文件及文件名进行更改,也可以整体移动到别的服务器执行
指定依赖项
from cx_Freeze import setup, Executable
# 指定需要包含的依赖项
includes = ["my_module", "my_package"]
# 指定不需要包含的依赖项
excludes = ["some_unwanted_module"]
# 设置setup参数
setup(
name = "MyApplication",
version = "0.1",
description = "My Python Application",
options = {
"build_exe": {
"includes": includes,
"excludes": excludes
}
},
executables = [
Executable("my_script.py", base = "Win32GUI")
]
)