PyInstaller 和多处理

use*_*533 3 python wxpython pyinstaller

我想从带有模块多处理的 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
Run Code Online (Sandbox Code Playgroud)

use*_*863 8

multiprocessing.freeze_support()在创建窗口之前将该行添加到您的代码中。它记录在这里