python - PyInstaller 和多处理

标签 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

最佳答案

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

关于python - PyInstaller 和多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870542/

相关文章:

python - django RequestFactory 丢失 url kwargs

python - 从 Windows 迁移到 Linux 时 WXPython 接口(interface)无法正常工作

python - 打包应用程序时如何排除不必要的Qt *.so 文件?

python - Py2app 阻止 wxpython 打开第二个 wx.Frame

python - wxPython 绑定(bind)问题

python - PyInstaller:无法排除模块

python - pyinstaller 缺少 mxnet dll

python - 格式字符串中的列表理解? (Python)

python - QTableWidget 选择多个单元格永久与 Ctrl + MouseClick 相同

python - 输出层中不兼容的形状 - Tensorflow