I'm trying to automate a large database process that is run at the beginning of every month. This is done, currently, with a bunch of stored procedures that are already in the database. My task is currently to get these stored procedures to run via Python's pyodbc.
The tricky part about these stored procedures is I need to run them one-at-a-time so they don't hog all of the database's resources. So, to check to see if the stored procedures have been run I created a table that is updated, changing a flag called "IsFinished" to False at the beginning of the procedure to True at the end.
The problem occurs when I try to query the database again. The activity monitor in SQL server is showing that my query against the Tracking table as SUSPENDED and blocked by the stored procedure call.
I've even gone so far as to create a new database so that I can call both of the databases independently just to further make sure I'm not competing for resources... but I still am competing for resources.
I'm using Python 3.6.4 and the pyodbc module. Here's my code. It should be noted that connect_db_1() and connect_db_2() point to two separate databases.
conn = connect_db_1()
conn.execute("{call dbo.StoredProcedure}")
conn2 = connect_db_2()
start_time = time.time()
finished = False
while finished is False:
print("Looping")
time.sleep(120)
cursor = conn2.execute(
"SELECT IsFinished from dbo.Tracking where StoredProcedureName='dbo.StoredProcedure'")
results = cursor.fetchall()
if results[0][0] == True:
finished = True
print(results)
print(f"dbo.StoredProcedure finished in {time_diff(start_time)} seconds.")
cursor.close()
EDIT: Added a sanitized version of my code.