[英]cx_Freeze executable runs multiple tasks when using multiprocessing and freeze_support
我用 cx_Freeze 構建了一個可執行文件。 我總是讀到我需要包含multiprocessing.freeze_support
以避免在任務管理器中運行的可執行文件的多個任務。 但是如果我使用 multiprocessing 和 freeze_support 我仍然會在任務管理器中運行兩個任務。
這是我名為test_wibu.py
的示例 GUI:
import multiprocessing
from multiprocessing import freeze_support
import threading
import queue
import tkinter as tk
import psutil
import time
from tkinter.filedialog import *
sys._enablelegacywindowsfsencoding()
def worker(pqueue):
while True:
obj = pqueue.get()
obj.execute()
del obj
if __name__ == '__main__':
freeze_support()
q = queue.Queue(maxsize=0)
root = Tk()
print('Doing something to build the software interface')
time.sleep(3)
label = Label(root, text='Software', anchor=CENTER)
label.grid(column=0, row=0, sticky='nwse', padx=0, pady=0)
pqueue = multiprocessing.Queue()
pool = multiprocessing.Pool(1, worker, (pqueue,))
parent = psutil.Process()
q.put('stop')
root.mainloop()
還有我的 setup_wibu.py:
import os.path
from cx_Freeze import *
PYTHON_INSTALL_DIR = os.path.join(os.getenv('LOCALAPPDATA'), 'Programs', 'Python', 'Python36')
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
executables = [
Executable('test_wibu.py',
base='Win32GUI',
targetName='test.exe',
)
]
options = {
'build_exe': {
'excludes': ['gtk', 'PyQt4', 'PyQt5', 'scipy.spatial.cKDTree', 'sqlite3', 'IPython'],
'packages': [],
'includes':['pkg_resources'],
'include_files': [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]
},
}
setup(
name='Pest_wibu',
version='1.0',
executables=executables,
options=options,
)
如果我構建可執行文件並運行它,我會在“詳細信息”中進入任務管理器中的兩個名為test.exe
的任務。 這是正常行為嗎? 如何避免可執行文件創建多個任務?
根據這個出色的答案,調用multiprocessing.freeze_support()
的原因是
Windows 上缺少
fork()
(這並不完全正確)。 因此,在 Windows 上,通過創建一個新進程來模擬分叉,其中正在運行 Linux 上正在子進程中運行的代碼。
因此,不要避免在您的前提下在任務管理器中運行的可執行文件的多個任務。
因此,您在任務管理器中觀察到的行為可能是正常的,無法避免。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.