Hey All!
I’m devising a strategy to prevent runs from just failing when there is another run in progress and wanted to see what others are doing along these same lines. Here is a chunk of code I use to have my run wait until it has an opening:
# Function to check if there is an active run
def check_active_run(thread_id):
try:
# Fetch runs for the thread and check their status
active_runs = client.beta.threads.runs.list(thread_id=thread_id)
for run in active_runs.data:
if run.status in ["in_progress", "queued"]:
return True
return False
except OpenAIError as e:
print(f"Failed to check runs: {str(e)}")
return False
# Wait until there is no active run
while check_active_run(thread.id):
print("Waiting for the existing run to complete...")
time.sleep(10) # Wait for 10 seconds before checking again
# Once no active runs are detected, proceed to create a new run
try:
print("Creating a new run...")
stream_run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
stream=True
)
Thoughts?