gpt4 book ai didi

python - Python 中的异常处理 - mysql.connector

转载 作者:行者123 更新时间:2023-11-29 17:22:49 25 4
gpt4 key购买 nike

我有 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:尝试获取锁时发现死锁并跳过其余的 while 循环,最后不更新时间,并在 0.2 秒后再次重新循环,如代码中所示。

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

try:
...
except:
continue
...

最佳答案

一种方法是在 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

关于python - Python 中的异常处理 - mysql.connector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51168661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com