Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyInstaller and multiprocessing

I want to start a simple HTTP-Server from a GUI (wxPython) with the module multiprocessing.

This code works fine if I start it directly with Python. But in the built version (with PyInstaller 2 or 3) the GUI starts again if I start the multiprogress -> not the code in the run-function, the whole application. Has anybody an idea why?

#!/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
like image 819
user3422533 Avatar asked Jan 08 '23 03:01

user3422533


1 Answers

Add the line multiprocessing.freeze_support() to your code before creating your window. It's documented here

like image 73
user2682863 Avatar answered Jan 18 '23 00:01

user2682863