Spaces:
Running
Running
Commit
·
771a66e
1
Parent(s):
012af0a
debugging
Browse files
app.py
CHANGED
|
@@ -114,20 +114,33 @@ async def process_fal_request(request_id, model_endpoint, fal_arguments, api_key
|
|
| 114 |
raise
|
| 115 |
|
| 116 |
def run_async_task(request_id, model_endpoint, fal_arguments, api_key):
|
| 117 |
-
"""Run async task
|
| 118 |
print(f"[DEBUG run_async_task] Starting for request {request_id}")
|
| 119 |
-
loop = asyncio.new_event_loop()
|
| 120 |
-
asyncio.set_event_loop(loop)
|
| 121 |
try:
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
print(f"[DEBUG run_async_task] Completed with result keys: {result.keys() if result else 'None'}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
except Exception as e:
|
| 125 |
print(f"[ERROR run_async_task] Failed: {str(e)}")
|
| 126 |
import traceback
|
| 127 |
traceback.print_exc()
|
| 128 |
-
finally:
|
| 129 |
-
loop.close()
|
| 130 |
-
print(f"[DEBUG run_async_task] Event loop closed for request {request_id}")
|
| 131 |
|
| 132 |
@app.route('/api/generate', methods=['POST'])
|
| 133 |
def generate():
|
|
|
|
| 114 |
raise
|
| 115 |
|
| 116 |
def run_async_task(request_id, model_endpoint, fal_arguments, api_key):
|
| 117 |
+
"""Run async task with proper event loop management"""
|
| 118 |
print(f"[DEBUG run_async_task] Starting for request {request_id}")
|
|
|
|
|
|
|
| 119 |
try:
|
| 120 |
+
# Use asyncio.run() which properly creates and closes a new event loop
|
| 121 |
+
# This avoids the "event loop is closed" error on subsequent requests
|
| 122 |
+
result = asyncio.run(process_fal_request(request_id, model_endpoint, fal_arguments, api_key))
|
| 123 |
print(f"[DEBUG run_async_task] Completed with result keys: {result.keys() if result else 'None'}")
|
| 124 |
+
except RuntimeError as e:
|
| 125 |
+
# Handle case where an event loop is already running (shouldn't happen with asyncio.run)
|
| 126 |
+
if "There is no current event loop" in str(e) or "Event loop is closed" in str(e):
|
| 127 |
+
print(f"[ERROR run_async_task] Event loop error, creating new loop: {str(e)}")
|
| 128 |
+
# Fallback: create a completely new event loop
|
| 129 |
+
loop = asyncio.new_event_loop()
|
| 130 |
+
try:
|
| 131 |
+
result = loop.run_until_complete(process_fal_request(request_id, model_endpoint, fal_arguments, api_key))
|
| 132 |
+
print(f"[DEBUG run_async_task] Completed with fallback loop")
|
| 133 |
+
finally:
|
| 134 |
+
loop.close()
|
| 135 |
+
else:
|
| 136 |
+
print(f"[ERROR run_async_task] Runtime error: {str(e)}")
|
| 137 |
+
import traceback
|
| 138 |
+
traceback.print_exc()
|
| 139 |
+
raise
|
| 140 |
except Exception as e:
|
| 141 |
print(f"[ERROR run_async_task] Failed: {str(e)}")
|
| 142 |
import traceback
|
| 143 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
@app.route('/api/generate', methods=['POST'])
|
| 146 |
def generate():
|