سؤال

This problem has been solved!

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!

edit: There is an error. ( http://pastebin.com/zHNG2c6U ) I have no idea what to do now..

webSocket Class:

class webSocket(WebSocketClientProtocol):
    def sendHello(self):
        pass

    def onOpen(self):
        self.sendHello()

    def onMessage(self, msg, binary):
        print "Got echo: " + msg
        reactor.callLater(1, self.sendHello)

    def notify(ignore, filepath, mask):
        print "CALLBACK"
        #print "event %s on %s" % (', '.join(inotify.humanReadableMask(mask)), filepath)

edit2: you are able to see the full code here: http://pastebin.com/iHKRcLVA

Final edit: Everyone, thanks for giving me response! Putting the callback 'def' out of the classes worked well!

هل كانت مفيدة؟

المحلول

Autobahn is built on Twisted, which is an asynchronous app framework. You don't need explicit threading to get this all working. You can instead implement a FileSystemWatcher class using twisted.internet.inotify (there's an example here).

I have no idea how the two components would talk to each other, since I haven't used Twisted for years. But there's an example of bridging to and from a serial port here.

نصائح أخرى

There is a complete demo for notify based watching and publishing over WebSocket in the Autobahn repo here.

This is for Windows, and runs the watching on a background thread. For Unix, you should follow Marcelo's advice and use something asynch which comes with Twisted.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top