pyqt5 opencv fastapi打包exe
一. pyinstaller 打包exe
下载upx压缩软件
常用参数介绍:
-D,–onedir:创建一个文件夹包含exe文件(默认)
-F,–onefile:打包之后只会生成一个exe的文件
-c,–console,–nowindowed:使用控制台窗口(默认)
-w,–windowed,–noconsole:使用窗口显示
-i:设置打包exe的图标,默认使用的是Python的图标
pip install pyinstaller
pyinstaller --upx-dir "upx" -F -w -i=panda.ico example.py
#--clean用于清除Pyinstaller的缓存
二. 打包fastapi
修改 .spec 文件
添加隐形导入
hiddenimports=['uvicorn.lifespan.off','uvicorn.lifespan.on','uvicorn.lifespan',
'uvicorn.protocols.websockets.auto','uvicorn.protocols.websockets.wsproto_impl',
'uvicorn.protocols.websockets_impl','uvicorn.protocols.http.auto',
'uvicorn.protocols.http.h11_impl','uvicorn.protocols.http.httptools_impl',
'uvicorn.protocols.websockets','uvicorn.protocols.http','uvicorn.protocols',
'uvicorn.loops.auto','uvicorn.loops.asyncio','uvicorn.loops.uvloop','uvicorn.loops',
'uvicorn.logging'],
重新生成
pyinstaller main.spec
三. pyqt5与fastapi 交互,线程信号和qt信号槽的灵活结合,获取返回值
信号槽,需要新建一个类
class MySignal(QtCore.QObject):
# sig_no_args = QtCore.pyqtSignal()
sig_with_str = QtCore.pyqtSignal(str,object)
连接处理信号函数
class mwindow(QWidget, Ui_Dialog):
def __init__(self):
super(mwindow, self).__init__()
self.signal = MySignal() # 信号槽
self.signal.sig_with_str.connect(self.controlsignal) # 信号控制]
self.info = None
'''
返回值
'''
def test(self,str):
# 线程信号
event = threading.Event()
self.signal.sig_with_str.emit(str,event)
# 异步阻塞
event.wait()
return self.info
'''
自定义信号管理
'''
def controlsignal(self,str,event):
self.info = "ok"
self.textEdit.append(str+"\n")
# 设置状态,取消阻塞
event.set()
fastapi 接口发送信号
app = FastAPI()
@app.get('/')
async def index(info:str):
data = m.test(info))
return data