Python-mysql.connector中的异常处理
我有两个pyton程序 1) “Prog1.py”处理数据库-从数据库查询 2) “Prog2.py”包含如下主运行循环Python-mysql.connector中的异常处理,python,mysql,exception-handling,mysql-python,Python,Mysql,Exception Handling,Mysql Python,我有两个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:
#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\u func\u 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中创建一个返回真值,如下所示:
def function():
try:
# your code here
#
return True
except:
#Exception codes
return False