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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM