5 months ago
Hi team,
I’m deploying a Django app on Railway with Gunicorn/Uvicorn as the ASGI server and Django Q for background tasks/scheduled jobs (push notifications, etc.).
Right now, I’m starting qcluster inside my container entrypoint.sh alongside Gunicorn. It kind of works, but I’m noticing:
Extremely slow response times on web requests (sometimes hanging for 1–2 minutes).
Logs suggest
qclusterit is competing with Gunicorn workers
I know the “ideal” solution is to run separate services (web and worker) like:
web: gunicorn fitease_backend.asgi:application -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:$PORT
worker: python manage.py qcluster
But that would count as two Railway services, which I’m trying to avoid for cost reasons.
This is my entrypoint script and below is my dockerfile.# Stage 1: Builder
FROM python:3.13 AS builder
RUN pip install pipenv
WORKDIR /app
COPY Pipfile Pipfile.lock ./
RUN pipenv install --deploy --system
# Stage 2: Final image
FROM python:3.13
WORKDIR /app
# Copy installed deps
COPY --from=builder /usr/local/lib/python3.13 /usr/local/lib/python3.13
COPY --from=builder /usr/local/bin /usr/local/bin
# Copy project files
COPY . .
RUN apt-get update && \
apt-get install -y --no-install-recommends ffmpeg libjemalloc2 && \
rm -rf /var/lib/apt/lists/*
ENV LD_PRELOAD=libjemalloc.so.2
# optional tuning
ENV MALLOC_CONF=background_thread:true,dirty_decay_ms:500,muzzy_decay_ms:500
# Copy entrypoint script
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
# Create non-root user
RUN useradd -m appuser
RUN chown -R appuser:appuser /app
USER appuser
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONPATH=/app
# Run your script first, then launch the app
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["gunicorn", "fitease_backend.asgi:application", "--bind", "0.0.0.0:8000", "-w", "1", "-k", "uvicorn_worker.UvicornWorker", "--max-requests", "2000", "--max-requests-jitter", "20"]
Pinned Solution
5 months ago
Solved this by using gunicorn config hook to run this process using subprocess module on the master gunicorn worker
2 Replies
5 months ago
Hey there! We've found the following might help you get unblocked faster:
🧵 FastAPI deployment failing - Error: '$PORT' is not a valid integer despite multiple CMD formats
🧵 Backend container exits after a few seconds even though Uvicorn starts correctly
If you find the answer from one of these, please let us know by solving the thread!
Status changed to Solved pratyaksh123 • 5 months ago
5 months ago
Solved this by using gunicorn config hook to run this process using subprocess module on the master gunicorn worker
Status changed to Awaiting Railway Response Railway • 5 months ago
Status changed to Solved brody • 5 months ago