Run a function once when a worker starts

I have some code that I only want to run once when my serverless worker starts (i.e not everytime a request is made). What is the best way of doing this? Just executing the code outside of the handler?

import runpod
import time

# this code runs once, on startup

time.sleep(10)
print("setup complete")

def handler(event):
    # handler code
    return {"message": "Hello World"}

runpod.serverless.start({"handler": handler})
Was this page helpful?