[英]Exception Handling in Python - mysql.connector
我有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()
处创建一个Error异常,在其中调用Prog1.py中的函数并引发错误mysql.connector.errors.InternalError : Deadlock found when try to get lock
并跳过while循环的其余部分mysql.connector.errors.InternalError : Deadlock found when try to get lock
,而不是最后更新时间,然后在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.