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

Viewed 3516

I already asked this exact question a year ago. My application uses Python's multiprocessing module, which works fine when run from the command line, but when I package it as an executable using Pyinstaller, the multiple processes spawn as new instances of the original rather than running the function they are supposed to run. The advice given to me last time as well as everywhere else I look is to call multiprocessing.freeze_support() at the beginning of my if __name__ == "__main__": block, which I have been doing this whole time, but I am suddenly running into this issue again for some reason. What else could possibly be causing this?

Update: I have confirmed that the presence of the freeze_support() line does not affect this issue at all. Commenting it or uncommenting it gives the exact same behavior: copies of the main window are opened and sit there doing nothing. Here is the block where it is called, at the extreme end of my main Python module:

if __name__ == '__main__':
    freeze_support()

    # (A bunch of commented-out lines)
    main()
3 Answers

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.

Additional gotcha for setup.py style programs that can cause this:

If you are using entrypoints to launch your program, you may need to put multiprocessing.freeze_support() inside main().

Pyinstaller_setuptools for example calls main() directly, skipping all statements in the if __name__ == '__main__': block.

Related