2014-09-06 55 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和线程。我已经多次使用这些文章中的技巧而没有问题。