How to load a heavy model when using MVC design pattern

前端 未结 3 1226
终归单人心
终归单人心 2021-01-17 06:34

I\'m building an application with wxPython and a Deep Learning model that I created using Tensorflow. The design pattern that I\'m using is MVC. My problem is that the deep

3条回答
  •  时光说笑
    2021-01-17 07:11

    You should call wx.GetApp().Yield() after the self.view.Show() command, which releases control momentarily to the MainLoop.
    If the model load code is performed in increments, you can call Yield periodically during the load as well.
    Below is a simple method of informing the user that something is going on. If you wanted the option of cancelling the model load, you would have to wrap it in a dialog, assuming that it is loaded incrementally.

    import wx
    import time
    
    class Model:
        def __init__(self):
            '''This part is simulating the loading of tensorflow'''
            x = 0
            #If the model load is perform in increments you could call wx.Yield
            # between the increments.
            while x < 15:
                time.sleep(1)
                wx.GetApp().Yield()
                print(x)
                x += 1
    
    class View(wx.Frame):
        def __init__(self, parent, title):
            super(View, self).__init__(parent, title=title, size=(400, 400))
            self.InitUI()
    
        def InitUI(self):
            # Defines the GUI controls
            #masterPanel = wx.Panel(self)
            #masterPanel.SetBackgroundColour("gold")
            self.vbox = wx.BoxSizer(wx.VERTICAL)
            self.fgs = wx.FlexGridSizer(6, 2, 10, 25)
            id = wx.StaticText(self, label="ID:")
            firstName = wx.StaticText(self, label="First name:")
            lastName = wx.StaticText(self, label="Last name:")
            self.idTc = wx.TextCtrl(self)
            self.firstNameTc = wx.TextCtrl(self)
            self.lastNameTc = wx.TextCtrl(self)
            self.fgs.AddMany([id, (self.idTc, 1, wx.EXPAND),
                             firstName, (self.firstNameTc, 1, wx.EXPAND),
                             lastName, (self.lastNameTc, 1, wx.EXPAND)])
            self.vbox.Add(self.fgs, proportion=1, flag=wx.ALL | wx.EXPAND,
       border=15)
            self.CreateStatusBar() # A Statusbar in the bottom of the window
            self.StatusBar.SetStatusText("No model loaded", 0)
            self.SetSizer(self.vbox)
            self.vbox.Fit(self)
            self.Layout()
    
    class Controller:
        def __init__(self):
            self.view = View(None, title='Test')
            self.view.Show()
            self.view.SetStatusText("Model loading", 0)
            wait = wx.BusyInfo("Please wait, loading model...")
            #Optionally add parent to centre message on self.view
            #wait = wx.BusyInfo("Please wait, loading model...", parent=self.view)
            wx.GetApp().Yield()
            self.model = Model()
            self.view.SetStatusText("Model loaded", 0)
            del wait
    
    def main():
        app = wx.App()
        controller = Controller()
        app.MainLoop()
    
    if __name__ == '__main__':
        main()
    

提交回复
热议问题