【发布时间】:2014-09-06 16:37:29
【问题描述】:
我最初想用 Python 创建一个带有看门狗和进程的文件监控系统。在我的程序中,我有一个 FileSystemEventHandler 子类 (DirectoryMonitorHandler) 来监视文件夹更改。声明了一个全局队列,以便DirectoryMonitorHandler 将项目插入到队列中。然后,队列可以是 Process 子类,用于处理由 DirectoryMonitorHandler 插入的对象。这是我程序中的代码 sn-p:
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 窗口如何与线程一起工作,但我需要它来与进程一起工作。另一种可能的解决方案是我必须在 Window 类中创建并启动 DirectoryMonitorHandler 和 BacklogManager 的实例。大家觉得呢?
【问题讨论】:
标签: python python-2.7 wxpython multiprocessing