Django容器突发重启报错:FileNotFoundError: /proc/393/task/558不存在
问题描述
我在AWS云环境中用Django 3.2.13和Python 3.9搭建后端服务,之前运行正常。最近前端发起常规请求时,前一个请求返回200状态码,但后端容器突然重启,抛出FileNotFoundError,提示找不到路径/proc/393/task/558。以下是相关信息,求解决思路:
错误回溯
Traceback (most recent call last): File "/app/manage.py", line 21, in <module> main() File "/app/manage.py", line 17, in main execute_from_command_line(sys.argv) File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 61, in execute super().execute(*args, **options) File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute output = self.handle(*args, **options) File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 96, in handle self.run(**options) File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 103, in run autoreload.run_with_reloader(self.inner_run, **options) File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 638, in run_with_reloader start_django(reloader, main_func, *args, **kwargs) File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 623, in start_django reloader.run(django_main_thread) File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 329, in run self.run_loop() File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 335, in run_loop next(ticker) File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 375, in tick for filepath, mtime in self.snapshot_files(): File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 391, in snapshot_files for file in self.watched_files(): File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 294, in watched_files yield from directory.glob(pattern) File "/usr/local/lib/python3.9/pathlib.py", line 1177, in glob for p in selector.select_from(self): File "/usr/local/lib/python3.9/pathlib.py", line 611, in _select_from for p in successor_select(starting_point, is_dir, exists, scandir): File "/usr/local/lib/python3.9/pathlib.py", line 558, in _select_from with scandir(parent_path) as scandir_it: FileNotFoundError: [Errno 2] No such file or directory: '/proc/393/task/558'
requirements.txt
aioredis==1.3.1 amqp==5.1.0 anyio==3.5.0 asgiref==3.5.0 async-timeout==4.0.2 attrs==21.4.0 autobahn==22.4.2 Automat==20.2.0 backports.zoneinfo==0.2.1 billiard==3.6.4.0 boto3==1.22.7 botocore==1.25.7 celery==5.2.6 certifi==2021.10.8 cffi==1.15.0 channels==3.0.4 channels-redis==3.4.0 charset-normalizer==2.0.12 click==8.1.2 click-didyoumean==0.3.0 click-plugins==1.1.1 click-repl==0.2.0 colorama==0.4.4 constantly==15.1.0 coreapi==2.3.3 coreschema==0.0.4 cryptography==37.0.2 daphne==3.0.2 Deprecated==1.2.13 Django==3.2.13 django-celery-beat==2.2.1 django-cors-headers==3.11.0 django-debug-toolbar==3.2.4 django-filter==21.1 django-redis==5.2.0 django-ses==3.0.1 django-storages==1.13.1 django-timezone-field==4.2.3 djangorestframework==3.13.1 djangorestframework-simplejwt==5.1.0 drf-yasg2==1.19.4 et-xmlfile==1.1.0 h11==0.13.0 hiredis==2.0.0 httptools==0.4.0 hyperlink==21.0.0 idna==3.3 incremental==21.3.0 inflection==0.5.1 itypes==1.2.0 Jinja2==3.1.1 jmespath==1.0.0 kombu==5.2.4 MarkupSafe==2.1.1 msgpack==1.0.3 numpy==1.22.4 openpyxl==3.0.10 packaging==21.3 pandas==1.4.2 prompt-toolkit==3.0.29 psycopg2-binary==2.9.3 pyasn1==0.4.8 pyasn1-modules==0.2.8 pycparser==2.21 PyJWT==2.3.0 pyOpenSSL==22.0.0 pyparsing==3.0.8 python-crontab==2.6.0 python-dateutil==2.8.2 python-decouple==3.6 python-dotenv==0.20.0 pytz==2022.1 PyYAML==6.0 redis==4.2.2 requests==2.27.1 ruamel.yaml==0.17.21 ruamel.yaml.clib==0.2.6 s3transfer==0.5.2 sentry-sdk==1.5.10 service-identity==21.1.0 six==1.16.0 sniffio==1.2.0 sqlparse==0.4.2 stripe==2.73.0 Twisted==22.4.0 txaio==22.2.1 typing_extensions==4.2.0 uritemplate==4.1.1 urllib3==1.26.9 uvicorn==0.17.6 uvloop==0.16.0 vine==5.0.0 watchgod==0.8.2 wcwidth==0.2.5 websockets==10.3 wrapt==1.14.0 zope.interface==5.4.0 yearfrac==0.4.7 openpyxl==3.0.10 Pillow==9.2.0
docker-compose配置
backend: image: backend:latest restart: always container_name: backend hostname: backend env_file: - ./.env volumes: - ./media:/app/media - ./static:/app/static command: bash -c "ls -la && python -u manage.py collectstatic --noinput && python -u manage.py migrate && python -u manage.py runserver 0.0.0.0:8000" depends_on: - redis
解决思路
1. 禁用Django自动重载功能
错误根源是Django的自动重载模块扫描文件时,尝试访问/proc下已销毁的进程任务目录。生产环境完全不需要自动重载,直接给runserver加--noreload参数:
修改docker-compose的command:
bash -c "ls -la && python -u manage.py collectstatic --noinput && python -u manage.py migrate && python -u manage.py runserver 0.0.0.0:8000 --noreload"
2. 切换到生产级服务器
runserver是Django的开发服务器,性能和稳定性都不适合生产场景。改用gunicorn或uvicorn:
- 在requirements.txt中添加
gunicorn==20.1.0 - 修改docker-compose的command:
bash -c "ls -la && python -u manage.py collectstatic --noinput && python -u manage.py migrate && gunicorn your_project_name.wsgi:application --bind 0.0.0.0:8000"
(注意把your_project_name替换成实际的项目名称)
3. 限制自动重载的扫描范围(仅测试环境用)
如果必须保留自动重载,可在settings.py中添加配置,让自动重载跳过/proc目录:
import django.utils.autoreload def skip_proc_files(sender, **kwargs): path = kwargs['file_path'] if '/proc/' in path: return True django.utils.autoreload.file_changed.connect(skip_proc_files, dispatch_uid='skip_proc_files')
4. 检查容器/proc挂载配置
AWS容器环境中/proc是虚拟挂载目录,可能存在权限或挂载不稳定的情况。可以检查ECS/EKS的任务定义,确认是否限制了/proc的访问权限,或者在Dockerfile中添加相关权限配置。
内容的提问来源于stack exchange,提问作者user20654086
相关产品推荐
相关产品推荐

