1

I need to run a stored procedure on an Azure SQL Managed Instance every 10 seconds from a Python application. The specific call to cursor.execute() happens in a class that extends threading.Thread like so:

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)

My current challenge is that the above thread does not recover gracefully from interruptions to network connectivity. My goal is to have the thread continue to run and re-attempt every 10 seconds until connectivity is restored, then recover gracefully.

  • Is the best practice here to delete and recreate the connection with every pass of the while loop?
  • Should I be using ConnectRetryCount or ConnectRetryInterval in my connection string?

While debugging I have found that even after connectivity is restored, pyodbc.connect() still fails with ODBC error 08S01 Communication link failure.

I have looked at the solution proposed in this post but don't see how to apply that solution to a continuous polling architecture.

George
  • 343
  • 2
  • 15

0 Answers0