pyinstaller打包多线程代码,运行死循环?

描述现象

用pyinstaller打包了一个上传文件的脚本,里面有多个input在while循环内,然后启用了多线程上传,在编辑器中运行没问题,但是打包完后,就一直循环提示你input...

解决

在google了一段时间后,发现需要在执行入口之前调用

multiprocessing.freeze_support()      # here

然后程序的运行就正常了。

python官网解释

multiprocessing.freeze_support()

Add support for when a program which uses multiprocessing has been frozen to produce a Windows executable. (Has been tested with py2exe, PyInstaller and cx_Freeze.)

One needs to call this function straight after the if name == ‘main‘ line of the main module. For example:

from multiprocessing import Process, freeze_support
def f():
    print('hello world!')

if __name__ == '__main__':
    freeze_support()
    Process(target=f).start()
If the freeze_support() line is omitted then trying to run the frozen executable will raise RuntimeError.

Calling freeze_support() has no effect when invoked on any operating system other than Windows. In addition, if the module is being run normally by the Python interpreter on Windows (the program has not been frozen), then freeze_support() has no effect.
posted @   丹华抱一鷇音子  阅读(1661)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示