HallD commited on
Commit
c57b0c6
Β·
verified Β·
1 Parent(s): 4402ed0

Update app.py

Browse files

modified code runner wrapper function

Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -2043,9 +2043,8 @@ def code_runner_wrapper(code_or_obj) -> str:
2043
  """
2044
  Wrapper for CodeRunnerAgent that uses async execution with warm pool.
2045
 
2046
- Provides a simplified interface to the code runner with automatic sandbox
2047
- pool management and user-friendly error messages. Handles warm-up status
2048
- checks and provides appropriate feedback during startup.
2049
 
2050
  Args:
2051
  code_or_obj: The code string or object to be executed
@@ -2055,23 +2054,27 @@ def code_runner_wrapper(code_or_obj) -> str:
2055
  """
2056
  try:
2057
  import asyncio
2058
-
2059
- # First check sandbox pool status to provide user feedback
2060
- try:
2061
- pool_status = asyncio.run(get_sandbox_pool_status())
 
 
2062
  user_message = pool_status.get("user_message", "")
2063
  if pool_status.get("status") == "warming_up":
2064
  return f"{user_message}\n\nPlease try again in a moment once the environment is ready."
2065
- except Exception:
2066
- pass # Continue with execution even if status check fails
2067
-
2068
- # Use async execution to leverage the warm sandbox pool
2069
- result = asyncio.run(code_runner.run_code_async(code_or_obj))
2070
- return result
2071
  except CodeExecutionError as e:
2072
  error_msg = str(e)
2073
  if "Failed to get sandbox" in error_msg or "timeout" in error_msg.lower():
2074
- return "πŸ”„ The code execution environment is still starting up. Please wait a moment and try again.\n\nThis is normal for the first execution after startup (can take 1-2 minutes)."
 
 
 
2075
  return error_msg
2076
  except Exception as e:
2077
  logger.error(f"Code runner wrapper error: {e}")
 
2043
  """
2044
  Wrapper for CodeRunnerAgent that uses async execution with warm pool.
2045
 
2046
+ Ensures a sandbox is spawned if not already present, waits for readiness,
2047
+ and then executes the code. Provides user-friendly error messages.
 
2048
 
2049
  Args:
2050
  code_or_obj: The code string or object to be executed
 
2054
  """
2055
  try:
2056
  import asyncio
2057
+
2058
+ async def ensure_and_run():
2059
+ # Ensure the sandbox pool is initialized and ready
2060
+ await code_runner._ensure_pool_initialized()
2061
+ # Wait for at least one sandbox to be available
2062
+ pool_status = await get_sandbox_pool_status()
2063
  user_message = pool_status.get("user_message", "")
2064
  if pool_status.get("status") == "warming_up":
2065
  return f"{user_message}\n\nPlease try again in a moment once the environment is ready."
2066
+ # Run the code in the sandbox
2067
+ return await code_runner.run_code_async(code_or_obj)
2068
+
2069
+ return asyncio.run(ensure_and_run())
2070
+
 
2071
  except CodeExecutionError as e:
2072
  error_msg = str(e)
2073
  if "Failed to get sandbox" in error_msg or "timeout" in error_msg.lower():
2074
+ return (
2075
+ "πŸ”„ The code execution environment is still starting up. Please wait a moment and try again.\n\n"
2076
+ "This is normal for the first execution after startup (can take 1-2 minutes)."
2077
+ )
2078
  return error_msg
2079
  except Exception as e:
2080
  logger.error(f"Code runner wrapper error: {e}")