twisted python - 通过 GPIO 发送消息,直到按下回车键才收到
twisted python - sending message via GPIO that isn't received until the enter key is pressed
我遇到了扭曲的问题python,我无法解决。
GPIO.add_event_detect(24, GPIO.RISING, callback=pDetected, bouncetime=1000)
def pDetected(channel):
communicator.sendNotifications(factory)
class notification(Protocol):
def connectionMade(self):
print "connection made but not added"
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
lineMessage = data.split('|')
theCommand = lineMessage[0]
theContent = lineMessage[1]
if theCommand == "welcome":
self.name = theContent
self.factory.clients.append(self)
print self.name + " has joined"
elif theCommand == "msg":
for c in self.factory.clients:
c.message(msg)
elif theCommand == "stopreactor":
reactor.stop()
def message(self, msgToSend):
msgToSend += " \r\n"
self.transport.write(msgToSend)
def sendNotifications(self, theFactory):
for c in theFactory.clients:
c.message("notify " + c.name)
factory = Factory()
factory.protocol = notification
factory.clients = []
communicator = notification()
reactor.listenTCP(myPort, factory)
reactor.run()
通知 class 中的所有内容都有效 - 客户端可以连接并且消息可以 sent/received 使用 telnet 没有问题。
当触发事件 pDetected 时,对 pDetected 的回调起作用。它发送消息(通知);然而,telnet 会话在我按下回车键之前不会收到消息......每次。 none 的其他客户端会看到消息,直到按下回车键。我 运行 端口上有一个分析器,但数据不在缓冲区中。
有人能指出我做错了什么吗?我希望 GPIO 事件触发并向连接到服务器的每个客户端发送消息。
感谢任何帮助...谢谢。
根据 this documentation,您传递给 GPIO.add_event_detect
的回调是 "threaded callback" - 意思是,它在新线程上运行。
从非主线程调用随机 Twisted API 是未定义的 - 您将获得随机行为。到目前为止,您所看到的是需要向它发送一条新消息以取消主循环,但其他情况也可能发生,包括挂起和崩溃。
解决这个问题的方法是 callFromThread
,像这样:
def pDetected(channel):
reactor.callFromThread(communicator.sendNotifications, factory)
GPIO.add_event_detect(24, GPIO.RISING, callback=pDetected, bouncetime=1000)
我遇到了扭曲的问题python,我无法解决。
GPIO.add_event_detect(24, GPIO.RISING, callback=pDetected, bouncetime=1000)
def pDetected(channel):
communicator.sendNotifications(factory)
class notification(Protocol):
def connectionMade(self):
print "connection made but not added"
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
lineMessage = data.split('|')
theCommand = lineMessage[0]
theContent = lineMessage[1]
if theCommand == "welcome":
self.name = theContent
self.factory.clients.append(self)
print self.name + " has joined"
elif theCommand == "msg":
for c in self.factory.clients:
c.message(msg)
elif theCommand == "stopreactor":
reactor.stop()
def message(self, msgToSend):
msgToSend += " \r\n"
self.transport.write(msgToSend)
def sendNotifications(self, theFactory):
for c in theFactory.clients:
c.message("notify " + c.name)
factory = Factory()
factory.protocol = notification
factory.clients = []
communicator = notification()
reactor.listenTCP(myPort, factory)
reactor.run()
通知 class 中的所有内容都有效 - 客户端可以连接并且消息可以 sent/received 使用 telnet 没有问题。
当触发事件 pDetected 时,对 pDetected 的回调起作用。它发送消息(通知);然而,telnet 会话在我按下回车键之前不会收到消息......每次。 none 的其他客户端会看到消息,直到按下回车键。我 运行 端口上有一个分析器,但数据不在缓冲区中。
有人能指出我做错了什么吗?我希望 GPIO 事件触发并向连接到服务器的每个客户端发送消息。
感谢任何帮助...谢谢。
根据 this documentation,您传递给 GPIO.add_event_detect
的回调是 "threaded callback" - 意思是,它在新线程上运行。
从非主线程调用随机 Twisted API 是未定义的 - 您将获得随机行为。到目前为止,您所看到的是需要向它发送一条新消息以取消主循环,但其他情况也可能发生,包括挂起和崩溃。
解决这个问题的方法是 callFromThread
,像这样:
def pDetected(channel):
reactor.callFromThread(communicator.sendNotifications, factory)
GPIO.add_event_detect(24, GPIO.RISING, callback=pDetected, bouncetime=1000)