odbc - 使用 pyodbc 管理数据库轮询连接的最佳实践?
问题描述
我需要每 10 秒从 Python 应用程序在 Azure SQL 托管实例上运行一个存储过程。特定的调用cursor.execute()
发生在一个threading.Thread
像这样扩展的类中:
class Parser(threading.Thread):
def __init__(self, name, event, interface, config):
threading.Thread.__init__(self)
self.name = name
self.stopped = event
self.interface = interface
self.config = config
self.connection_string = config['connection_string']
self.cnxn = pyodbc.connect(self.connection_string)
def run(self):
while not self.stopped.wait(10):
try:
cursor = self.cnxn.cursor()
cursor.execute("exec dbo.myStoredProcedure")
except Exception as e:
logging.error(e)
我当前的挑战是上述线程无法从网络连接中断中正常恢复。我的目标是让线程继续运行并每 10 秒重新尝试一次,直到连接恢复,然后正常恢复。
- 此处的最佳做法是在每次
while
循环时删除并重新创建连接吗? - 我应该使用
ConnectRetryCount
还是ConnectRetryInterval
在我的连接字符串中?
在调试时我发现即使在连接恢复后, pyodbc.connect() 仍然失败并出现 ODBC 错误08S01 Communication link failure
。
我查看了这篇文章中提出的解决方案,但没有看到如何将该解决方案应用于连续轮询架构。
解决方案
推荐阅读
- powershell - 如何选择字符串多行?
- javascript - 为什么我看不到函数“Data.Feature.setId(id)”?谷歌地图 JavaScript API
- flutter - android:label 属性不影响颤振应用程序名称
- amazon-web-services - 安装了 Mosquito 的 AWS EC2 不起作用
- xml - 针对外部模式的 XML 验证
- java - 数据表不显示值
- python - 使用串行将浮点数从arduino发送到python
- php - TYPO3:如何使用 typeNum=20 创建自定义页面类型 url 并在控制器上引用它
- python - Ansible 找不到 python 模块
- java - 有没有办法从没有存储库和实体的表中获取数据?