简体   繁体   中英

Why does this AssertionError exception not continue

What I Have

I posted the correlated question Is there a method to help make this Python logic run faster a few days ago and I've extended on this logic for my testing needs after getting a confirmed solution.

For some reason I cannot figure out how to tell this Python as it's running if it gets an AssertionError to print out Assertion Error and then continue back with the logic again.

from cpppo.server.enip.get_attribute import proxy_simple
from datetime import datetime
import time, csv, sys

host = "<PLC IP Address>"
TagName = "Tag Name"
RootCsvFile = "X:\\Some\\Folder\\Path\\"
source = proxy_simple(host)
prev = None

def csvwrite(v):
    with open(v[2],"a",newline='') as file:
        csv_file = csv.writer(file)
        csv_file.writerow([v[0],v[1]])

def ExecLoop(pr):
    prev = pr   
    while True:

        for message in source.read(TagName):

            try:
                val = message[0]
                timestr = str(datetime.now())
                if val != prev:
                    prev = val
                    YYYYMMDD = datetime.today().strftime('%Y%m%d')
                    CsvFile = RootCsvFile + TagName + "_" + YYYYMMDD + ".csv"
                    print(timestr, val, CsvFile)
                    csvwrite([timestr, val, CsvFile])

            except AssertionError:
                print("Assertion Error")
                continue           
ExecLoop(prev)

The Problem

Since this is getting data from a PLC machine network via a VPN connection, whenever the VPN has a disruption and is not able to be accessed via the Python, the below error occurs and it seems like the except AssertionError: is not doing what I've asked it to do and just get an error:

Traceback (most recent call last):   File
"~\GetTag-ToSQLDB&CSVEFile.py", line 63, in <module>
      ExecLoop(prev)   File "~\GetTag-ToSQLDB&CSVEFile.py", line 46, in ExecLoop
      for message in source.read(TagName):   File "~\Python\Python36-32\lib\site-packages\cpppo\server\enip\get_attribute.py",
line 431, in read
      for val,(sts,(att,typ,uni)) in reader:   File "~\Python\Python36-32\lib\site-packages\cpppo\server\enip\get_attribute.py",
line 606, in read_details
       depth=self.depth, multiple=self.multiple, timeout=self.timeout )): File
"~\Python\Python36-32\lib\site-packages\cpppo\server\enip\client.py",
line 1393, in operate

  for idx,dsc,req,rpy,sts,val in harvested:   File "~\Python\Python36-32\lib\site-packages\cpppo\server\enip\client.py",
line 1270, in pipeline 
complete, requests ) 

AssertionError: Communication ceased before harvesting all pipeline responses:   0/  1

What I'd Like

I'd like for it to keep retrying whenever there is an AssertionError since typically when there is an issue with connectivity, it's a short few second blip or else a couple minutes for an automated weekly power cycle of a network device on the client side of the VPN which the Python is running.

  • My Question: How can I get the except AssertionError: logic in this process to print something and then go back and try looping or whatever again?

    • At the moment of the error, the process halt and has the traceback detail on the screen as posted in The Problem section of this question.

My Assumptions

I'm assuming the problem is related to:

  • The AssertionError handling being defined in the client.py file as specified in the traceback error and that superseding the scripted except AssertionError: logic in the script.

  • The Try: and Exception: logic being within a while True loop and then within a for loop within the while True loop.


Other Details (Just In Case)

  • This process is using the cpppo package.

  • I'm using Python version 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] and from Windows 10 .

  • I am running this process right from IDLE as I'm testing and not as a compiled package or anything like that. So the logic I posted is saved as a py file and then when opened in IDLE I execute it pressing F5 .

  • The "assumed" correlated client.py logic related to the AssertionError (see line 1269):

    • The logic shared here clienty.py since too big for my post.

The AssertionError occured in the source.read(TagName) call. you need to wrap this with your try except block:

while True:
    try:
        for message in source.read(TagName):   # Note that this connection might be broken after the error. 
            val = message[0]
            timestr = str(datetime.now())
            if val != prev:
                prev = val
                YYYYMMDD = datetime.today().strftime('%Y%m%d')
                CsvFile = RootCsvFile + TagName + "_" + YYYYMMDD + ".csv"                       print(timestr, val, CsvFile)
                csvwrite([timestr, val, CsvFile])
     except AssertionError:
         print("Assertion Error")
         continue  

The traceback says it all: your except block is not in a place where it can see the exception at all. Specifically, look at the line

 for message in source.read(TagName): File "~\\Python\\Python36-32\\lib\\site-packages\\cpppo\\server\\enip\\get_attribute.py", line 431, in read 

The error is in your for loop, but the try is inside the loop. You had the right idea wrapping everything in a while True . The solution is to move the for inside the try .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM