如何让MQTT客户端在Appengine(python)上工作



尝试在付费GAE 应用程序上运行简单的 MQTT 客户端(而不是代理)。但是,在以下情况下永远不会发生on_connect回调:

worker.py

import webapp2
import paho.mqtt.client as paho
class WorkerHandler(webapp2.RequestHandler):
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
def on_connect(client, userdata, flags, rc):
client.subscribe("$SYS/#")
print "Subscribed to wildcard"
def get(self):
client = paho.Client()
client.on_connect = self.on_connect
client.on_subscribe = self.on_subscribe
client.on_message = self.on_message
client.connect("iot.eclipse.org")
print "connected to broker"
client.loop_forever()
app = webapp2.WSGIApplication([
(r'/_ah/start', WorkerHandler),
])

在开发环境中,它在一分钟左右后仅显示一条消息以静默方式失败

INFO     2017-04-04 01:51:40,958 module.py:813] worker: "GET /_ah/start HTTP/1.1" 500 220
INFO     2017-04-04 01:51:41,869 module.py:1759] New instance for module "worker" serving on: http://localhost:8080
connected to broker
WARNING  2017-04-04 01:52:10,860 module.py:1927] All instances may not have restarted

这被配置为"后端"/服务,yaml 如下所示:

工人.yaml

service: worker
runtime: python27
api_version: 1
threadsafe: true
instance_class: B8
manual_scaling:
instances: 1
handlers:
# If a service has an _ah/start handler, it should be listed first.
- url: /_ah/start
script: worker.app

注意:在开发环境中,socket.py 直接从python install导入.../2.7/lib/python2.7/socket.py

您正在尝试将独立脚本作为 GAE 应用worker服务运行。这是行不通的。

您的worker.py需要包含一个名为app的 WSGI 应用程序以匹配您的worker.yaml配置。

从处理程序元素表中的script行:

脚本:指令必须是 python 导入路径,例如,package.module.app指向WSGI应用程序。使用Python 模块路径的脚本:指令的最后一个组件是 模块中全局变量的名称:该变量必须是 WSGI应用程序,通常按照惯例称为应用程序

您收到的错误很可能表示尝试启动辅助角色模块 WSGI 应用失败。

更新以恢复WSGI应用程序后,错误消息的原因变得更加清晰:WorkerHandler.get()方法不响应/_ah/start请求,因为它卡在client.loop_forever()中。

从启动:

每个服务实例都是为响应启动请求而创建的,该请求 是一个空的 HTTP GET 请求/_ah/start。应用引擎发送此内容 请求使实例存在;用户无法发送 请求/_ah/start.手动和基本扩展实例必须 在他们可以处理另一个请求之前响应启动请求。

当实例使用 HTTP 响应/_ah/start请求时 状态码为200–299404,则认为已成功 已启动,可以处理其他请求。否则,应用引擎 终止实例。重启手动扩缩容实例 立即,而基本扩展实例仅在以下情况下重新启动 需要为流量提供服务。

最新更新