首页 > 解决方案 > 在 docker 中使用 gunicorn 运行 Django 并重新加载会产生 TypeError

问题描述

Django 版本:2.2.23

一切正常。但是,当我尝试将--reload标志与 gunicorn 一起使用时,会导致以下错误:

[2021-06-08 09:48:07 +0000] [79] [INFO] Starting gunicorn 19.7.1
[2021-06-08 09:48:07 +0000] [79] [INFO] Listening at: http://0.0.0.0:8000 (79)
[2021-06-08 09:48:07 +0000] [79] [INFO] Using worker: sync
/usr/lib/python3.8/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
  return io.open(fd, *args, **kwargs)
[2021-06-08 09:48:07 +0000] [81] [INFO] Booting worker with pid: 81
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/reloader.py", line 42, in run
    for filename in self.get_files():
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/reloader.py", line 28, in get_files
    fnames = [
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/reloader.py", line 29, in <listcomp>
    re.sub('py[co]$', 'py', module.__file__)
  File "/usr/lib/python3.8/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

这是我的码头文件:

#20.04-LTS
FROM ubuntu:focal
RUN apt update

# install python
RUN apt install -y git python python3 python3-dev python3-pip python3-virtualenv && pip3 install --upgrade pip

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

RUN mkdir /web
WORKDIR /web
ADD requirements.txt /web/
RUN pip3 install -r requirements.txt
COPY . /web

我的码头工人撰写:

version: '2'
services:
  web:
    build: .
    depends_on:
      - db
      - redis
    volumes:
      - .:/web
    ports:
      - "8000:8000"
    command: bash -c "./entrypoint.sh"

我的 entrypoint.sh:

#!/bin/bash

# Collect static files
echo "Collecting static files"
python3 manage.py collectstatic --noinput

# Apply database migrations
echo "Applying database migrations"
python3 manage.py migrate

# Start server
echo "Starting server"
gunicorn app.wsgi -b  0.0.0.0:8000 --reload

标签: djangodockerdjango-rest-frameworkdocker-composegunicorn

解决方案


推荐阅读