Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Python asyncio

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Python asyncio's example analysis, aiming at this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Let's start with a common Python programming error, which I've seen a lot of programmers make:

Def do_not_raise (user_defined_logic): try: user_defined_logic () except: logger.warning ("User defined logic raises an exception", exc_info=True) # ignore

What's wrong with this code?

Let's start with the abnormal structure of Python. There are two exception base classes in Python, the most basic of which is BaseException, and the second is Exception (inheriting BaseException). What's the difference between the two?

Exception represents most of the exceptions that we often handle in our business logic, including some running errors such as NameError, AttributeError, and so on. But not all exceptions are subclasses of the Exception class, and a few exceptions inherit from BaseException:

GeneratorExit

SystemExit

KeyboardInterrupt

The first representative generator is closed by the close () method, the second represents the system exit (for example, using sys.exit), and the third representative program is interrupted by Ctrl+C. They do not inherit from Exception because they should never be captured in general, or should be reraise immediately after they are captured (through a raise statement with no parameters).

If you write a statement like that, the program may not be able to exit: the SIGTERM signal is sent to the program from the outside, triggering the SystemExit, but the SystemExit is captured and ignored, so that the program does not exit normally, but continues to execute. Exceptions such as SystemExit, KeyboardInterrupt, GeneratorExit, etc., because there is no fixed throw position, so it is very dangerous if caught indiscriminately, it is likely to produce implicit bug, and it will be difficult to find in the test. This is why the official Python documentation stresses that if you use except with no parameters, be sure to re-throw the exception with raise. The correct way to ignore the execution of exceptions should be:

Def do_not_raise (user_defined_logic): try: user_defined_logic () except Exception: #

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report