IotEdge Python模块不显示任何日志

时间:2020-09-02 15:12:17

标签: python azure

您好,我已按照教程在此处创建演示:https://docs.microsoft.com/en-us/azure/iot-edge/tutorial-machine-learning-edge-01-intro

我有用于python ML模块的docker文件:

FROM mcr.microsoft.com/azureml/o16n-base/python-slim:latest.northeurope

RUN apt-get update && \
    apt-get install -y gcc g++ cmake

RUN conda update conda -y

RUN mkdir -p '/var/azureml-app' WORKDIR /var/azureml-app COPY myenv.yml .

RUN conda env create -f myenv.yml

COPY . .

SHELL ["conda", "run", "-n", "myenv", "/bin/bash", "-c"]

ENTRYPOINT ["conda", "run", "-n", "myenv", "python", "-u", "main.py"]

然后我有这个main.py文件

import score
import time
import os
import sys
import asyncio
from six.moves import input
import threading
import json
import logging
from azure.iot.device.aio import IoTHubModuleClient
from azure.iot.device import Message

INPUT_ENDPOINT_NAME = "amlInput"
OUTPUT_ENDPOINT_NAME = "amloutput"

L = None

def writeLog(data):
    global L
    if L == None:
        L = logging.getLogger(__name__)
        handler = logging.StreamHandler()
        formatter = logging.Formatter('%(asctime)s [%(name)s] %(levelname)-8s %(message)s')
        handler.setFormatter(formatter)
        L.addHandler(handler)
        L.setLevel(logging.DEBUG)

    L.info(data)


async def main():
    try:
        writeLog( "IoT Hub Client for Python" )
        module_client = IoTHubModuleClient.create_from_edge_environment()
        await module_client.connect()

        # define behavior for receiving an input message on input1
        async def input1_listener(module_client):
            while True:
                input_message = await module_client.receive_message_on_input(INPUT_ENDPOINT_NAME)  # blocking call
                writeLog(f"the data in the message received on {INPUT_ENDPOINT_NAME} was: {input_message.data}")

                message_buffer = input_message.get_bytearray()
                size = len(message_buffer)
                message_data = json.loads(message_buffer[:size].decode('utf-8'))

                result = score.run(message_data)
                writeLog(f"Machine learned: {result}")

                output_message = Message(json.dumps(result))
                await module_client.send_message_to_output(output_message, OUTPUT_ENDPOINT_NAME)

        # define behavior for halting the application
        def stdin_listener():
            while True:
                try:
                    selection = input("Press Q to quit\n")
                    if selection == "Q" or selection == "q":
                        writeLog("Quitting...")
                        break
                except:
                    time.sleep(10)

        # Schedule task for C2D Listener
        listeners = asyncio.gather(input1_listener(module_client))

        writeLog( "MLmodule is now waiting for messages. ")

        # Run the stdin listener in the event loop
        loop = asyncio.get_event_loop()
        user_finished = loop.run_in_executor(None, stdin_listener)

        # Wait for user to indicate they are done listening for messages
        await user_finished

        listeners.cancel()
        await module_client.disconnect()

    except Exception as e:
        writeLog( f"Unexpected error {e}" )
        raise


if __name__ == "__main__":
    writeLog("MLModule Booting Python")
    score.init()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

我已经设置了turbofanrouter,我认为路线还可以,并且还可以正确发送消息 问题是我docker logs <MLModule container>时看不到任何日志 所以我无法调试 解决办法是什么?谢谢

0 个答案:

没有答案