godisreal / CrowdEgress

Multi-Agent Simulation of Pedestrian Crowd by Using Extended Social Force Model
Apache License 2.0
9 stars 0 forks source link

Use multiprocessing to coordinate Tkinter with Pygame #2

Open godisreal opened 4 years ago

godisreal commented 4 years ago

I am trying to use multiprocessing to coordinate Tkinter with Pygame. Here GUI is created by Tkinter as the father process. When users start the simulation, GUI creates a son process of pygame and GUI process is suspended. After pygame exits, GUI process is free to run again, and users can set up another simulation and then start another simulation run.

In other words, there is one token to coordinate two processes. When the son process of pygame is created, the token is automatically given to pygame. When pygame process is closed by users, the token is returned to father process of Tkinter.

One problem is that I found I do not have to use .join() in multiprocessing after the pygame process terminates. The whole program runs more smoothly if I do not use .join() in multiprocessing and I do not know why. I will keep exploring this topic.

    def startSim(self):
        self.currentSimu = simulation()
        self.currentSimu.select_file(self.fname_EVAC, self.fname_FDS, "non-gui")
        #self.textInformation.insert(END, "Start Simulation Now!")
        self.setStatusStr("Simulation starts!  GUI window is not effective when Pygame screen is displayed!")
        self.updateCtrlParam()
        self.currentSimu.preprocessGeom()
        self.currentSimu.preprocessAgent()
        sunpro1 = mp.Process(target=show_simu(self.currentSimu))  # Pygame starts here      
        sunpro1.start()
        #sunpro1.join()
        self.setStatusStr("Simulation not yet started!")
        self.currentSimu.quit()