Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

python - pyInstaller loads script multiple times

I've managed to get pyinstaller to run more or less correctly now, except that it opens too many windows. It's pygame project, and it keeps loading the entire thing over again every second or so. My PC fills with game windows after a few seconds and everything grinds to a halt.

Running it from commandline, I can just see print outs saying the app is starting pasted over and over again in commandline window. As far as I can tell the Apps aren't closing or exiting, just spawning more and more.

The command I call to start pyinstaller is this:

pyinstaller --onedir main_local.py

The main file looks like this:

# Library imports
import pygame

# Project imports
from multiroidal_client    import MultiroidalClient
from settings              import *
import time

# Main program function
def main():

    # Initialise game class
    game = MultiroidalClient(SCREEN_SIZE, ('127.0.0.1', 8080))

    # Start game loop
    game.main_game_loop()


# Execute main function
if __name__ == '__main__':
    main()

I tried commenting out the if __ name __ ... bit to see if for some reason it was executing the main function and calling a duplicate each time by accident or something. When commented out the exec does nothing, as you might expect.

Any ideas? I've not included any more code because there is loads of it, and I'm not sure which parts are relevant. Let me know if you need to see anything else.

Cheers

EDIT: I added a quick print after the game.main_game_loop() just to check if the script jumped out of the infinite game loop. No such luck. It's loading a parallel instances of the scripts, all running simultaneously.

EDIT: I looked through the pyinstaller docs and managed to get some more debug out. Not sure if it's relevant or not, but here it is. This screen of dialog just keeps reprinting over and over again. I also tried the --noupx option which can sometimes help apparently - no such luck.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Multiprocessing!

I tried some simpler python programs, and they packaged up all fine. So it wasn't a problem with my method it was the code. Thinking about it again, it must be some unusual or difficult to package code in the project. Hmmm, maybe threading?

I've got a couple of threads running in parallel, and they are multiprocessing threads. After googling about I found this magic fix.

You simply stick this line, multiprocessing.freeze_support(), straight after if __name__ == "__main__" and before you call the main() function.

if __name__ == '__main__':

    # Pyinstaller fix
    multiprocessing.freeze_support()

    main()

It looks like the guys who develop the multiprocessing module had to include a hack to allow freeze packaging (py2exe, pyinstaller etc). Seems weird it isn't included a bit more smoothly. If you leave that freeze_support() call in, it still works even when you aren't running from packaged code as opposed to standard python files.

Anyway, if you are freezing/pacakaging code up and it seems to execute itself over and over, check if you are use multiprocessing. A simple google "multiprocessing pyinstaller" gave the answer.

FYI this only works on --onedir mode, --onefile mode is a different story and not supported on windows. You'll have to dig around more to solve that one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...