Multiprocessing runs new instances of the main window when frozen as an executable

I am an idiot. (Well, maybe not, but it was entirely my fault) multiprocessing signals a process that it is a child process by running it with two additional arguments: the flag --multiprocessing-fork and a numerical handle to a pipe from the parent process. multiprocessing.freeze_support checks for the presence of this flag to decide whether to run the function specified for the child process, or the normal program. Anyway, my method of parsing command-line arguments altered sys.argv, which got rid of the flag and caused the child processes to act like new parent processes.

So, the moral of the story is, never alter sys.argv. I switched to use optparse, which complains about the presence of the flag so I had to pass it a filtered list of arguments. Once I did this, the issue vanished.


I have had the same problem but I have solved putting if clauses in the code to avoid every kivy code to run in a new process. For example:

if __name__ == '__main__': # to avoid new window with a new process
    multiprocessing.freeze_support() # support multiprocessing in pyinstaller
    from kivy.lang.builder import Builder
    from kivy.clock import Clock
    from kivy.uix.popup import Popup
    from kivy.uix.textinput import TextInput
    from kivy.uix.scatterlayout import ScatterLayout
    from kivy.graphics.transformation import Matrix
    from kivy.uix.scatter import Scatter
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import BooleanProperty, ListProperty, BoundedNumericProperty, StringProperty, NumericProperty
    from kivy.uix.boxlayout import BoxLayout
    from kivy.app import App
    from kivy.uix.behaviors import ButtonBehavior
    from kivy.uix.image import Image
    from kivy.core.window import Window

Beyond that, all my classes that use kivy have needed to get put inside the same if clause to avoid errors in main program.

My conclusion is some interaction of kivy, multiprocessing and pyinstaller make the new window pop. If running the code direct from python it doesn't show the problem. In my case, I already was using one if clause to import kivy.core.window, and it has worked fine running the code direct from python, but not after pyinstaller, even using freeze_support.

I hope it helps someone.