我最初想用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
的實例,並在啓動之前將它傳遞到DirectoryMonitorHandler
和BacklogManager
。
我也讀過這個http://wiki.wxpython.org/LongRunningTasks,它解釋了一個wxPython窗口如何與線程一起工作,但我需要它來處理一個Process。另一個可能的解決方案是,我將不得不在Window
類中創建並啓動DirectoryMonitorHandler
和BacklogManager
的實例。你們有什麼感想?