refactor(rq_worker.py): put rq worker behind function call (prevent default import)

This commit is contained in:
Krrish Dholakia 2023-11-21 13:51:42 -08:00
parent 68c955409d
commit 5c3ea2a97e

View file

@ -1,6 +1,4 @@
import sys, os import sys, os
from rq import Worker, Queue, Connection
from redis import Redis
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
# Add the path to the local folder to sys.path # Add the path to the local folder to sys.path
@ -8,23 +6,16 @@ sys.path.insert(
0, os.path.abspath("../../..") 0, os.path.abspath("../../..")
) # Adds the parent directory to the system path - for litellm local dev ) # Adds the parent directory to the system path - for litellm local dev
def start_rq_worker():
# # Import your local module from rq import Worker, Queue, Connection
# import litellm from redis import Redis
# from litellm import litellm_queue_completion # Set up RQ connection
redis_conn = Redis(host=os.getenv("REDIS_HOST"), port=os.getenv("REDIS_PORT"), password=os.getenv("REDIS_PASSWORD"))
# Set up RQ connection print(redis_conn.ping()) # Should print True if connected successfully
redis_conn = Redis(host=os.getenv("REDIS_HOST"), port=os.getenv("REDIS_PORT"), password=os.getenv("REDIS_PASSWORD")) # Create a worker and add the queue
print(redis_conn.ping()) # Should print True if connected successfully try:
# Create a worker and add the queue queue = Queue(connection=redis_conn)
try: worker = Worker([queue], connection=redis_conn)
queue = Queue(connection=redis_conn) except Exception as e:
worker = Worker([queue], connection=redis_conn) print(f"Error setting up worker: {e}")
except Exception as e: exit()
print(f"Error setting up worker: {e}")
exit()
# Run the worker
if __name__ == '__main__':
with Connection(redis_conn):
worker.work()