Python 3.10 subprocess error: FileNotFoundError: [Errno 2] No such file or directory: 'pkill'

Python 3.10 subprocess error: FileNotFoundError: [Errno 2] No such file or directory: 'pkill'

我正在尝试在检测到代码更改时自动重新加载作为 Django 应用程序一部分的 celery。我正在使用以下管理命令启动芹菜:

"""
https://avilpage.com/2017/05/how-to-auto-reload-celery-workers-in-development.html
"""

import shlex
import subprocess

from django.core.management.base import BaseCommand
from django.utils import autoreload


def restart_celery_worker():
    cmd = "pkill -9 celery"
    subprocess.call(shlex.split(cmd))
    cmd = "celery -A my_app.celery_app worker -l info --concurrency=2"
    subprocess.call(shlex.split(cmd))


class Command(BaseCommand):
    def handle(self, *args, **options):
        print("Starting celery worker with autoreload...")
        autoreload.run_with_reloader(restart_celery_worker)

我看到以下错误:

Starting celery worker with autoreload...
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.10/threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/code/core/management/commands/start_worker_local.py", line 14, in restart_celery_worker
    subprocess.call(shlex.split(cmd))
  File "/usr/local/lib/python3.10/subprocess.py", line 345, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/local/lib/python3.10/subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.10/subprocess.py", line 1842, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'pkill'

我在尝试更新到 Python 3.10 时开始看到这个问题。这是我正在使用的 Dockerfile:

FROM python:3.10.0-slim-buster
ENV LANG C.UTF-8

ENV DEBIAN_FRONTEND=noninteractive

RUN mkdir -m 775 /code

ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/code

WORKDIR /code

RUN python3 -m pip install --upgrade pip setuptools
RUN pip install pipenv
COPY Pipfile ./
RUN pipenv lock --python /usr/local/bin/python
RUN pipenv install --system --deploy --dev

COPY app /code

pkillprocps package 的一部分,可以通过以下方式安装:

apt-get update && apt-get install procps

您可以通过以下方式确认 procps 包未包含在 python:3.10.0-slim-buster 图像中:

$ docker run --rm python:3.10.0-slim-buster dpkg --get-selections | grep procps

这是因为 slim 图像变体不包含 procps 包。但是,我们可以确认 procps 包包含在 buster 图像中:

$ docker run --rm python:3.10.0-buster dpkg --get-selections | grep procps
libprocps7:amd64                install
procps                          install