简体   繁体   中英

How to parallel to processes at a time using python

I researched first and couldn't find an answer to my question. I am trying to run my another program with GUI parallel with GUI that I made in Python by wxpython. def that I want to emphasize on is def "startsumo" to parallel with def "onSurasakPhase3(event):" . The thing is that whenever I click on "button2" which is lead to def "startsumo" and the function of it is to run another GUI called by subprocess, then I will not be able to click on bmp button at the same time. What I know is that it is a sequential program and I have to find a way to do it in parallel instead of a series. Please help.

I have something like this:

class top(wx.Frame):

    def __init__(self,parent,id):
    wx.Frame.__init__(self,parent,id,'Frane aka window', size=(500,500))
    panel=wx.Panel(self)
    button=wx.Button(panel,label="exit",pos=(10,10),size=(60,60))
    self.Bind(wx.EVT_BUTTON, self.closebutton, button)
    self.Bind(wx.EVT_CLOSE, self.closewindow)

    button2=wx.Button(panel,label="START_SUMO",pos=(100,10),size=(100,60))
    self.Bind(wx.EVT_BUTTON, self.startsumo, button2)

    def onSurasakPhase3(event):
        print "surasak phase 3 is selected"

    bmp = wx.Bitmap("surasak_phase3.png", wx.BITMAP_TYPE_ANY)
    buttonOnSurasakPhase3 = wx.BitmapButton(panel, id=wx.ID_ANY, bitmap=bmp,
                              size=(bmp.GetWidth(), bmp.GetHeight()))
    buttonOnSurasakPhase3.Bind(wx.EVT_BUTTON, onSurasakPhase3)
    buttonOnSurasakPhase3.SetPosition((2*(bmp.GetWidth())+20,30))


    def startsumo(self,event):
        def run():
            traci.init(PORT)
            step = 0
            while step < 15200:
               traci.simulationStep()

               step += 1

        sumoProcess = subprocess.Popen(["sumo-gui ", "-c", "sathon_s_lefthand.sumo.cfg ","-a", "TESTTTT.add.xml ",
                                         "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)

        run()
        sumoProcess.wait()

    def closebutton(self,event):
        self.Close(True)

    def closewindow(self, event):

        self.Destroy()

if __name__=='__main__':
app=wx.PySimpleApp()
frame=top(parent=None,id=-1)
frame.Show()
app.MainLoop()

Your main thread is waiting until the subprocess is completed, because of the call to sumoProcess.wait() in startsumo(). If you want your GUI to be responsive, you need to not do this wait there.

Try the following replacement for your def startsumo

def startsumo(self,event):
    traci.init(PORT)
    step = 0
    while step < 15200:
        traci.simulationStep()
        step += 1
    sumoProcess = subprocess.Popen(["sumo-gui ", "-c", "sathon_s_lefthand.sumo.cfg ","-a", "TESTTTT.add.xml ",
                                     "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr).pid

That should run without tying up your current process.
sumoProcess will contain the pid of the running program should you need to kill it for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM