SoFunction
Updated on 2024-12-14

How to rewind to the crash after a Python program reports an error and crashes (recommended)

Suppose we have a program that reads data from Redis, parses it, and extracts the name field:

import json
import redis
client = ()
def read():
  while True:
    data = ('info')
    if data:
      yield (data)
    else:
      break
def parse():
  for data in ():
    print(data['name'])

if __name__ == '__main__':
  parse()

The logic of the code itself is very simple, it reads the data from Redis one by one, and the data read is a JSON string, so it is first parsed into a dictionary. Then it reads the value corresponding to the name in the dictionary. It keeps reading until the Redis list is empty.

Let's run it and see:

An error has been reported, indicating that there is a problem with a piece of data in Redis. You want to take a look at the problematic data, but now the program has crashed, the process has ended, and the problematic data is lost forever. You'll never know what it looks like again.

Those of you who have played Fire Emblem - Windswept know that the protagonist has a skill called the Pulse of Heavenly Carving, which allows him to reverse time and go back to before his teammates were killed if they die, thus changing their fate.

So, is there anything we can do inside Python to bring a program back from the dead and see the line of code that caused it to report an error in the first place? If you're running the program using python3, then indeed, there's no way to know unless you can re-import the data you just did.

However, if you are a program started with the following command: python3 -i, then the world is different and your program gains the ability to come back from the dead. You can get back to the scene of the accident.

Let's recover the Redis data (of course, you may not be able to recover it inside the production environment. But I can still reply to the example data for writing the article now ^_^)

Then re-run the program using python3 -i read_name.py:

As you can see, although the program now crashes, the Python interactive environment appears. The process has not completely exited. This allows us to enter the magic command to rewind the program back to the same place where the error was reported. Enter the command:

import ()

The running effect is shown below:

Now we're back to the line that reported the error. The error is that the dictionary data doesn't have the key name, so let's see what's in the dictionary by typing in the variable name data.

As it turns out, the problematic piece of data was writing name as name1.

summarize

pdb is the debugging tool that comes with Python. The debugging features of PyCharm, which we use, are also implemented based on pdb.

to this article on the Python program error crash how to fall back to the location of the crash of the article is introduced to this, more related Python program error crash content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!