我想使用模块多处理从 GUI (wxPython) 启动一个简单的 HTTP 服务器。
如果我直接用 Python 启动这段代码,它工作正常。但是在构建版本(使用 PyInstaller 2 或 3)中,如果我启动 multiprogress -> 不是运行函数中的代码,而是整个应用程序,GUI 会再次启动。 有人知道为什么吗?
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import wx, sys, time, thread, datetime, os, platform, multiprocessing, socket
import favicon
from genLicense import load as loadLicense
from licenseDetailDialog import Dialog as licenseDetailDialog
class mp(multiprocessing.Process):
def __init__(self, queue, func, *args):
multiprocessing.Process.__init__(self)
self.queue = queue
self.func = func
self.args = args
def run(self):
time.sleep(0.1)
try:
self.func(*self.args)
except Exception as e:
self.queue.put(e)
print(e)
class MainFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER
wx.Frame.__init__(self, *args, **kwds)
# [...]
self.button_startstop = wx.Button(self, wx.ID_ANY, _("Server &starten"))
# [...]
def startstop(self, event):
if self.running:
self._stop()
else:
self._start()
def _start(self):
print("Starting...")
try:
port = 123
queue = multiprocessing.Queue()
self.server_process = mp(queue, ACC_main.START, port)
self.server_process.start()
print("\tPID: {}\n\n{}".format(self.server_process.pid, "="*50))
# [...]
self.running = True
except:
# [...]
def _stop(self):
print("\n{}\nStopping...".format("="*50))
if self.running:
print("\tPID: {}\n".format(self.server_process.pid))
self.server_process.terminate()
self.running = False
最佳答案
在创建窗口之前将 multiprocessing.freeze_support()
行添加到您的代码中。已记录 here
关于python - PyInstaller 和多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870542/