linux

Linux系统中Celery的资源限制如何设置

小樊
82
2024-09-14 10:52:19
栏目: 智能运维

在 Linux 系统中,可以使用 systemdsupervisord 等工具来管理 Celery 进程

  1. 使用 systemd 设置资源限制:

首先,创建一个名为 celery.service 的 Systemd 服务文件。将以下内容添加到 /etc/systemd/system/celery.service

[Unit]
Description=Celery Service
After=network.target

[Service]
Type=forking
User=your_user
Group=your_group
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/your/virtualenv/bin/celery multi start worker -A your_project.celery --pidfile=/var/run/celery/%n.pid --logfile=/var/log/celery/%n%I.log --loglevel=INFO
ExecStop=/path/to/your/virtualenv/bin/celery multi stopwait worker --pidfile=/var/run/celery/%n.pid
ExecReload=/path/to/your/virtualenv/bin/celery multi restart worker -A your_project.celery --pidfile=/var/run/celery/%n.pid --logfile=/var/log/celery/%n%I.log --loglevel=INFO

LimitNOFILE=10240
LimitNPROC=1024
LimitMEMLOCK=infinity
LimitSTACK=infinity
LimitCORE=infinity
LimitRSS=infinity
LimitAS=infinity
LimitCPU=infinity

[Install]
WantedBy=multi-user.target

这里的 Limit* 参数定义了 Celery 进程的资源限制。根据需要调整这些值。

然后,重新加载 Systemd 配置并启动 Celery 服务:

sudo systemctl daemon-reload
sudo systemctl enable celery
sudo systemctl start celery
  1. 使用 supervisord 设置资源限制:

首先,安装 Supervisor:

sudo apt-get install supervisor

接下来,创建一个名为 celery.conf 的 Supervisor 配置文件。将以下内容添加到 /etc/supervisor/conf.d/celery.conf

[program:celery]
command=/path/to/your/virtualenv/bin/celery -A your_project.celery worker --loglevel=INFO
directory=/path/to/your/project
user=your_user
numprocs=1
stdout_logfile=/var/log/celery/worker.log
stderr_logfile=/var/log/celery/worker.error.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
stopasgroup=true
priority=998

[program:celerybeat]
command=/path/to/your/virtualenv/bin/celery -A your_project.celery beat --loglevel=INFO
directory=/path/to/your/project
user=your_user
numprocs=1
stdout_logfile=/var/log/celery/beat.log
stderr_logfile=/var/log/celery/beat.error.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
stopasgroup=true
priority=999

接下来,更新 Supervisor 配置并启动 Celery 进程:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start celery

注意:Supervisor 不支持直接设置资源限制。但是,你可以通过在命令行中使用 ulimit 命令来设置资源限制。例如,要设置最大打开文件数限制为 10240,可以在 command 参数中添加 ulimit -n 10240 &&

command=ulimit -n 10240 && /path/to/your/virtualenv/bin/celery -A your_project.celery worker --loglevel=INFO

请根据需要调整资源限制。

0
看了该问题的人还看了