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})
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})
1 Reply
neural-soupe
neural-soupeOP6d ago
Overview | RunPod Documentation
Create and deploy serverless Handler Functions with RunPod, processing submitted inputs and generating output without managing server infrastructure, ideal for efficient, cost-effective, and rapid deployment of code.

Did you find this page helpful?