我正在尝试学习 AWS greengrass,因此我正在关注本教程https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-gs.html,它逐步解释了在 raspberry pi 上设置 greengrass并使用 lambda 函数发布一些消息。
一个简单的 lambda 函数如下:
import greengrasssdk
import platform
from threading import Timer
import time
# Creating a greengrass core sdk client
client = greengrasssdk.client('iot-data')
# Retrieving platform information to send from Greengrass Core
my_platform = platform.platform()
def greengrass_hello_world_run():
if not my_platform:
client.publish(topic='hello/world', payload='hello Sent from Greengrass Core.')
else:
client.publish(topic='hello/world', payload='hello Sent from Greengrass Core running on platform: {}'.format(my_platform))
# Asynchronously schedule this function to be run again in 5 seconds
Timer(5, greengrass_hello_world_run).start()
# Execute the function above
greengrass_hello_world_run()
# This is a dummy handler and will not be invoked
# Instead the code above will be executed in an infinite loop for our example
def function_handler(event, context):
return
在这里这是可行的,但我试图通过使用 lambda 函数来做一些额外的工作来更好地理解它,例如打开一个文件并写入它。
我修改了greengrass_hello_world_run()
功能如下
def greengrass_hello_world_run():
if not my_platform:
client.publish(topic='hello/world', payload='hello Sent from Greengrass Core.')
else:
stdout = "hello from greengrass\n"
with open('/home/pi/log', 'w') as file:
for line in stdout:
file.write(line)
client.publish(topic='hello/world', payload='hello Sent from Greengrass Core running on platform: {}'.format(my_platform))
我希望在部署时,在我的本地 pi 上运行的守护程序应该在给定目录中创建该文件,因为我相信 greengrass 核心会尝试在本地设备上运行这个 lambda 函数。但是它不会创建任何文件,也不会发布任何内容,因为我相信这段代码可能会被破坏。不知道如何,我尝试查看 cloudwatch,但我没有看到任何事件或错误报告。
对此的任何帮助将不胜感激,干杯!