Reputation: 1173
I initially wanted to create a file monitoring system with Python with watchdog and Process. In my program, I have a FileSystemEventHandler
subclass (DirectoryMonitorHandler
) to monitor for folder changes. A global queue is declared so that DirectoryMonitorHandler
inserts items into a queue. The queue is then can Process subclass that is used to process the object that was inserted by the DirectoryMonitorHandler
. Here is a code snippet from my program:
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)
This program works fine, but now I've decided to add a GUI component to it. Here is what I have so far:
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()
This window works fine, but I am not sure how I can integrate these two components together. Should the wxPython GUI be running as a separate process? I was thinking that I could create an instance of MonitorWindow
, pass it into DirectoryMonitorHandler
and BacklogManager
before they are started.
I have also read this http://wiki.wxpython.org/LongRunningTasks which does explain how a wxPython window works with threads, but I am in need of it to work with a Process. Another possible solution would be that I will have to create and start instance of DirectoryMonitorHandler
and BacklogManager
from within the Window
class. What do you guys think?
Upvotes: 2
Views: 572
Reputation: 33071
wxPython itself should be the main process and it should launch the long running process that monitors your file system. If you are using the multiprocessing module, then you should probably read one of the following:
The Long Running Tasks wiki article that you mentioned is great for learning how to use wxPython and threads. I have used the techniques in that articles several times without issues.
Upvotes: 1