【问题标题】:How to safely stop an infinite loop to finish gathering Json data in Python?如何安全地停止无限循环以完成在 Python 中收集 Json 数据?
【发布时间】:2021-05-06 02:47:02
【问题描述】:

我有一个在无限循环上运行的脚本,从 Arduino 收集数据并将这些信息添加到 json 文件中。当我用 cnt c 停止程序时,它会切断 while 循环,因此没有正确完成将数据发送到 json 文件。

我查看了其他堆栈溢出问题。就像我曾经在我的代码中实现的 "[How to stop an infinite loop safely in Python?"][1] 一样,但它有时会正确终止 json 数据。

我的 while 循环看起来像这样:

interrupted = False

while True:
    serialDataDic= ArduinoSerial(arduinoSerial,68,startTimeofData)# gather x amount of samples of data at 8.9kHz
    # print(serialDataDic)
    

    
    decompositionCoeffiecients=DWT(serialDataDic['voltage'],'haar',2)
    # print(decompositionCoeffiecients)
   
   #looping through each of our functions and placing that info in our dictionary to go to our json file  
    for key in serialDataDic:
        plottingDicData[key].append(serialDataDic[key])
    for key in decompositionCoeffiecients:
        plottingDicData[key].append(decompositionCoeffiecients[key])
    # print(plottingDicData)
    #sending out sensor data into a json file 
    jsonfileData=JsonData(fileName,plottingDicData)
    
    if interrupted:
        print("Gotta go")
        break

如何正确终止此 while 循环,以便在终止前完成 json 数据收集?

【问题讨论】:

    标签: python-3.x infinite-loop


    【解决方案1】:

    我认为您可能想要某种形式的 try 循环与 except 行结合使用,该行明确捕获键盘中断,然后使用 sys.exit() 方法在最后强制退出(以及发送包含在括号)。

    这样,您的脚本将在 try 循环下正常运行,然后 KeyboardInterrupt 将捕获您的 ctrl-C,但将其强制为您想要的,然后强制退出。

    这当然可以清理一下,但希望这很清楚。

    import sys
    
    while True:
        try:
            serialDataDic= ArduinoSerial(arduinoSerial,68,startTimeofData)# gather x amount of samples of data at 8.9kHz
            # print(serialDataDic)
            decompositionCoeffiecients=DWT(serialDataDic['voltage'],'haar',2)
            # print(decompositionCoeffiecients)
       
            #looping through each of our functions and placing that info in our dictionary to go to our json file  
            for key in serialDataDic:
                plottingDicData[key].append(serialDataDic[key])
            for key in decompositionCoeffiecients:
                plottingDicData[key].append(decompositionCoeffiecients[key])
            # print(plottingDicData)
            #sending out sensor data into a json file 
            jsonfileData=JsonData(fileName,plottingDicData)
        
        except KeyboardInterrupt:
            decompositionCoeffiecients=DWT(serialDataDic['voltage'],'haar',2)
            # print(decompositionCoeffiecients)
       
            #looping through each of our functions and placing that info in our dictionary to go to our json file  
            for key in serialDataDic:
                plottingDicData[key].append(serialDataDic[key])
            for key in decompositionCoeffiecients:
                plottingDicData[key].append(decompositionCoeffiecients[key])
            # print(plottingDicData)
            #sending out sensor data into a json file 
            jsonfileData=JsonData(fileName,plottingDicData)
            # The below line force quits and prints the included string
            sys.exit("User manually terminated script with ctrl-C")
    
    

    【讨论】:

    • 好吧,我试一试,我唯一担心的是,当我试图用 ctrl-c 结束 while 循环时,它会立即削减内容,因此无法正确完成 json 数据收集。这不会做同样的事情吗?
    • 很高兴它有帮助!正如我所提到的,可能有一种更优雅的方法来解决这个问题。现在tryexcept KeyboardInterrupt 块重复自己,这有点草率。也许finally 块可能会有所帮助。没有更多细节很难说。快乐编码。 :)
    • 所以只是为了澄清一下,键盘中断是否像一个标志一样。因此,如果我在 try 块内的 for 循环中仍然执行时按 ctrl C,它不会中断该循环,它会完成它然后检查中断,然后转到 except 块?
    • 没有。如果您按control-c,那么您将手动引发KeyboardInterrupt 错误。这相当于你的 for 循环 inside 有一个错误。在这种情况下,当您引发 KeyboardInterrupt 错误时,您的代码在 try 块(或 for 循环)内的任何位置,它将停止并检查破坏 try 块的错误与 @987654333 @ 状况。由于您指定了KeyboardInterrupt,它会捕获该错误,然后执行该异常块下的内容。如果仍然感到困惑,我会看看这个real python article
    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多