Search code examples
pythonmultiprocessingpyinstaller

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


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()

Solution

  • 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.