Skip to main content

Running Code Locally

Before deploying your serverless functions to the cloud, it's crucial to test them locally. In the previous lesson, Hello World with RunPod, you created a Python file called hello_world.py.

In this guide, you'll learn how to run your RunPod serverless applications on your local machine using the RunPod Python SDK.

Understanding RunPod's Local Testing Environment

When you run your code locally using the RunPod Python SDK, here's what happens behind the scenes:

  • FastAPI Server: The SDK spins up a FastAPI server on your local machine. This server simulates the RunPod serverless environment.
  • Request Handling: The FastAPI server receives and processes requests just like the cloud version would, allowing you to test your function's input handling and output generation.
  • Environment Simulation: The local setup mimics key aspects of the RunPod serverless environment, helping ensure your code will behave similarly when deployed.

Running Your Code Locally

Let's walk through how to run your serverless functions locally using the RunPod Python SDK.

Options for Passing Information to Your API

The RunPod Python SDK offers two main methods for sending data to your local FastAPI server:

  1. Using a JSON file
  2. Using inline JSON via command line

Both methods allow you to simulate how your function would receive data in the actual cloud environment.

Using a JSON File

  1. Create a JSON file:

    Create a file called test_input.json with your test data:

    {
    "input": {
    "name": "World"
    }
    }
  2. Run the serverless function:

    Execute your hello_world.py script with the --rp_server_api flag:

    python hello_world.py --rp_server_api

    The SDK will automatically look for and use the test_input.json file in the current directory.

Using Inline JSON

You can also pass your test data directly via the command line:

python hello_world.py --test_input '{"input": {"name": "World"}}'

This method is useful for quick tests or when you want to vary the input without editing a file.

Understanding the output

When you run your function locally, you'll see output similar to this:

--- Starting Serverless Worker |  Version 1.6.2 ---
INFO | Using test_input.json as job input.
DEBUG | Retrieved local job: {'input': {'name': 'World'}, 'id': 'local_test'}
INFO | local_test | Started.
DEBUG | local_test | Handler output: Hello World!
DEBUG | local_test | run_job return: {'output': 'Hello World!'}
INFO | Job local_test completed successfully.
INFO | Job result: {'output': 'Hello World!'}
INFO | Local testing complete, exiting.

This output provides valuable information:

  • Confirmation that the Serverless Worker started successfully
  • Details about the input data being used
  • Step-by-step execution of your function
  • The final output and job status

By analyzing this output, you can verify that your function is behaving as expected and debug any issues that arise.

Key Takeaways

  • Local testing with the RunPod Python SDK allows you to simulate the cloud environment on your machine.
  • The SDK creates a FastAPI server to mock the serverless function execution.
  • You can provide input data via a JSON file or inline JSON in the command line.
  • Local testing accelerates development, reduces costs, and helps catch issues early.

Next, we'll explore the structure of RunPod handlers in more depth, enabling you to create more sophisticated serverless functions.