Python: two loops at once
- by Stephan Meijer
I've got a problem: I am new to Python and I want to do multiple loops. I want to run a WebSocket client (Autobahn) and I want to run a loop which shows the filed which are edited in a specific folder (pyinotify or else Watchdog).
Both are running forever, Great. Is there a way to run them at once and send a message via the WebSocket connection while I'm running the FileSystemWatcher, like with callbacks, multithreading, multiprocessing or just separate files?
factory = WebSocketClientFactory("ws://localhost:8888/ws", debug=False)
factory.protocol = self.webSocket
connectWS(factory)
reactor.run()
If we run this, it will have success. But if we run this:
factory = WebSocketClientFactory("ws://localhost:8888/ws", debug=False)
factory.protocol = self.webSocket
connectWS(factory)
reactor.run()
# Websocket client running now,running the filewatcher
wm = pyinotify.WatchManager()
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
print "Creating:", event.pathname
def process_IN_DELETE(self, event):
print "Removing:", event.pathname
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch('/tmp', mask, rec=True)
notifier.loop()
This will create 2 loops, but since we already have a loop, the code after 'reactor.run()' will not run at all..
For your information: this project is going to be a sync client.
Thanks a lot!