Python: two loops at once

Posted by Stephan Meijer on Stack Overflow See other posts from Stack Overflow or by Stephan Meijer
Published on 2013-11-02T09:16:02Z Indexed on 2013/11/02 9:54 UTC
Read the original article Hit count: 259

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!

© Stack Overflow or respective owner

Related posts about python

Related posts about multithreading