【问题标题】:Exception Handling in Python - mysql.connectorPython 中的异常处理 - mysql.connector
【发布时间】:2018-07-04 08:02:27
【问题描述】:

我有 2 个 pyton 程序 1) 处理数据库的“Prog1.py” - 从数据库中查询 2) 'Prog2.py' 包含如下的主运行循环

#importing the database class from Prog1.py (mysql.connector used to in Prog1.py)
from database import Database
...

#main run loop
while(True):
   time.sleep(0.2)
   for loc in data:
         self.datafunc(loc)
         call_func_fromprg1()
         foo()
         bar()
    #not to run these conditions if exception is met
    if expression1:
        then operation1
    if expression1:
        then operation2
    if expression3:
        then operation3
    if expression4:
        then operation4

    var = time()

我正在尝试在 call_func_fromprg1() 创建一个错误异常,其中调用 Prog1.py 中的函数并引发错误 mysql.connector.errors.InternalError : Deadlock found when try to get lock 并跳过 while 循环的其余部分,最后不更新时间,并在 0.2 秒后重新循环,如代码中给出的那样。

我需要的是写以下子句的最佳位置

try:
...
except:
 continue
...

【问题讨论】:

    标签: python mysql exception-handling mysql-python


    【解决方案1】:

    一种方法是在 prog2.py 中创建一个状态变量,如下所示。

    from database import Database
    ...
    
    #main run loop
    while(True):
       time.sleep(0.2)
       for loc in data:
             self.datafunc(loc)
             status = call_func_fromprg1()
             foo()
             bar()
        #not to run these conditions if exception is met
        if expression1:
            then operation1
        if expression1:
            then operation2
        if expression3:
            then operation3
        if expression4:
            then operation4
        if status:
            var = time()
    

    并在 prog1.py 中创建一个返回 True 值,如下所示:

    def function():
        try:
            # your code here
            #
            return True
        except:
            #Exception codes
            return False
    

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多