Note that there are some explanatory texts on larger screens.

plurals
  1. POCall a twisted protocol method from another thread
    primarykey
    data
    text
    <p>I have made a Home Security program in Python that uses Raspberry Pi's GPIOs to sense movement and actuate the siren. The users activate/deactivate the system using a NFC Tag to a nfc reder connected also to raspberry pi.</p> <p>For this I need to constantly check for nfc tags in a non blocking manner and at the same time constantly check the sensors for movement also non blocking. I need some more parallel stuff to do but I think these two are enough to make my point.</p> <p>Right now I use threads which I start/stop like this - <a href="https://stackoverflow.com/questions/6524459/stopping-a-thread-python">Stopping a thread after a certain amount of time</a> - I'm not sure if this is the optimal way but as of now the system works fine.</p> <p>Now I want to extend its functionality to offer notifications through websockets. I found that this can be done with Twisted but I am confused..</p> <p>Here is an example code of how I am trying to do it:</p> <pre><code>from twisted.internet import reactor from autobahn.websocket import WebSocketServerFactory, \ WebSocketServerProtocol, \ listenWS def thread1(stop_event): while(not stop_event.is_set()): stop_event.wait(4) print "checking sensor" # sensor_state = GPIO.input(11) if sensor_state == 1: # how can I call send_m("sensor detected movement") #&lt;--- t1_stop_event.set() t1_stop_event = Event() t1 = Thread(target=thread1, args=(t1_stop_event,)) class EchoServerProtocol(WebSocketServerProtocol): def onMessage(self, msg, binary): print "received: "+msg print "stopping thread1" t1_stop_event.set() def send_m(self, msg): self.sendMessage(msg) if __name__ == '__main__': t1.start() factory = WebSocketServerFactory("ws://localhost:9000") factory.protocol = EchoServerProtocol listenWS(factory) reactor.run() </code></pre> <p>So how can I call the send method of the server protocol from a thread like the thread1?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload