2014-09-06 50 views
2

我最初想用Python創建一個帶有看門狗和Process的文件監控系統。在我的程序中,我有一個FileSystemEventHandler子類(DirectoryMonitorHandler)來監視文件夾更改。聲明全局隊列,以便DirectoryMonitorHandler將項插入到隊列中。然後,該隊列可以處理用於處理由DirectoryMonitorHandler插入的對象的子類。這裏是我的程序代碼片段:wxPython GUI與現有流程

if __name__ == "__main__": 
    # The global queue that is shared by the DirectoryMonitorHandler and BacklogManager is created 
    q = Queue() 

    # Check if source directory exists 
    if os.path.exists(source): 
     # DirectoryMonitorHandler is initialized with the global queue as the input 
     monitor_handler = DirectoryMonitorHandler(q) 
     monitor = polling.PollingObserver() 
     monitor.schedule(monitor_handler, source, recursive = True) 
     monitor.start() 

     # BacklogManager is initialized with the global queue as the input 
     mamanger = BacklogManager(q) 
     mamanger.start() 

     mamanger.join() 
     monitor.join() 
    else: 
     handleError('config.ini', Error.SourceError) 

該程序工作正常,但現在我決定添加一個GUI組件。以下是我迄今爲止:

class MonitorWindow(wx.Frame): 
    def __init__(self, parent): 
     super(MonitorWindow, self).__init__(parent, title='Monitor', size=(size_width, size_height)) 
     staticBox = wx.StaticBox(self, label='Monitor Status:', pos=(5, 105), size=(size_width - 28, size_height/3)) 
     self.statusLabel = wx.StaticText(staticBox, label='None', pos=(10, 35), size=(size_width, 20), style=wx.ALIGN_LEFT) 
     self.InitUI() 

    def InitUI(self): 
     panel = wx.Panel(self) 
     self.SetBackgroundColour(background_color) 
     self.Centre() 
     self.Show() 

    def updateStatus(self, status): 
     self.statusLabel.SetLabel('Update: ' + status) 

if __name__ == '__main__': 
    app = wx.App() 
    window = MonitorWindow(None) 
    app.MainLoop() 

該窗口工作正常,但我不知道我怎麼可以在這兩個組件集成在一起。 wxPython GUI應該作爲一個單獨的進程運行嗎?我在想,我可以創建一個MonitorWindow的實例,並在啓動之前將它傳遞到DirectoryMonitorHandlerBacklogManager

我也讀過這個http://wiki.wxpython.org/LongRunningTasks,它解釋了一個wxPython窗口如何與線程一起工作,但我需要它來處理一個Process。另一個可能的解決方案是,我將不得不在Window類中創建並啓動DirectoryMonitorHandlerBacklogManager的實例。你們有什麼感想?

回答

1

wxPython本身應該是主進程,它應該啓動監視文件系統的長時間運行的進程。如果您使用的是多模塊,那麼你或許應該閱讀下列之一:

,你提到的長時間運行的任務wiki文章是偉大的學習如何使用wxPython和線程。我已經多次使用這些文章中的技巧而沒有問題。