我最初想用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
的实例。你们有什么感想?