Python 如何在生产中将 celery 作为守护进程运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14275821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:57:06  来源:igfitidea点击:

How to run celery as a daemon in production?

pythondjangocelery

提问by Hick

i created a celeryd file in /etc/defaults/ from the code here:

我从这里的代码在 /etc/defaults/ 中创建了一个 celeryd 文件:

https://github.com/celery/celery/blob/3.0/extra/generic-init.d/celeryd

https://github.com/celery/celery/blob/3.0/extra/generic-init.d/celeryd

Now when I want to run celeryd as a daemon and do this: sudo /etc/init.d/celerdy it says command not found. Where am I going wrong?

现在,当我想将 celeryd 作为守护程序运行并执行以下操作时: sudo /etc/init.d/celedy 它说找不到命令。我哪里错了?

采纳答案by Rohan

I am not sure what you are doing here but these are the steps to run celery as a daemon.

我不确定你在这里做什么,但这些是将 celery 作为守护进程运行的步骤。

  1. The file that you have referred in the link https://github.com/celery/celery/blob/3.0/extra/generic-init.d/celerydneeds to be copied in your /etc/init.dfolder with the name celeryd
  2. Then you need to create a configuration file in the folder /etc/defaultwith the name celerydthat is used by the above script. This configuration file basically defines certain variables and paths that are used by the above script. Here's an example configuration.
  3. This link Generic init scriptsexplains the process and can be used for reference
  1. 您在链接https://github.com/celery/celery/blob/3.0/extra/generic-init.d/celeryd 中引用的文件 需要/etc/init.d使用名称复制到您的文件夹中 celeryd
  2. 然后,您需要在/etc/default具有celeryd上述脚本使用的名称 的文件夹中创建一个配置文件 。该配置文件基本上定义了上述脚本使用的某些变量和路径。这是一个示例配置。
  3. 这个链接Generic init scripts解释了这个过程,可以参考

回答by michel.iamit

I found this link extremely useful: How to write an Ubuntu Upstart job for Celery (django-celery) in a virtualenv

我发现这个链接非常有用:How to write an Ubuntu Upstart job for Celery (django-celery) in a virtualenv

tweaking it a bit.. I have a celery worker running using this script:

稍微调整一下..我有一个使用这个脚本运行的芹菜工人:

(using ubuntu upstart)

(使用 ubuntu 新贵)

named iamcelery.conf and placed it in /etc/init (note: not init.d)

命名 iamcelery.conf 并将其放在 /etc/init (注意:不是 init.d)

# iamcelery -runs the celery worker as my virtual env user
#
#
# This task is run on startup to start the celery worker as my vritual env user

description "runs the celery worker"
author "michel van Leeuwen <[email protected]>"

start on runlevel [2345]
stop on runlevel [!2345]

# retry if ended unexpectedly
respawn
# limit the retries to max 15 times with timeouts of 5 seconds
respawn limit 15 5

# Time to wait between sending TERM and KILL signals
kill timeout 20

task
script
  exec su -s /bin/sh -c 'exec "
sudo start iamcelery
" "$@"' <place here your unprovilegd username> -- srv/<here the path of your django project>/bin/django celeryd -BE -l info end script

now you can start this scipt (it starts on server startup as well):

现在你可以启动这个 scipt(它也在服务器启动时启动):

sudo stop iamcelery

or stop:

或停止:

sudo status iamcelery

or check its status:

或检查其状态:

script
  su <place here your unprovilegd username>
  cd /srv/<here the path of your django project>/
  exec bin/django celeryd -BE -l info
end script

I am not quit sure this is the neatest way.... however... after a long trial and errors trying to get the initd scripts to work.... ( without succes) ... this finally works.

我不确定这是最简洁的方法......但是......经过长时间的试验和错误试图让initd脚本工作......(没有成功)......这终于奏效了。

Edit 8 june 2013My script given here seemed to runs as a root in the end. Now I changed this:

编辑 2013 年 6 月 8 日我在这里给出的脚本最终似乎以 root 身份运行。现在我改变了这个:

script
  exec su -s /bin/sh -c 'exec "
[watcher:celery]
cmd = full_path/python3.4 full_path/manage.py celeryd -B -l info

[watcher:celerycamera]
cmd = full_path/python3.4 full_path/manage.py celery events --camera=djcelery.snapshot.Camera

[watcher:dceleryflower]
cmd = full_path/python3.4 full_path/manage.py celery flower -A your_app_name --basic_auth=username:password --port=5555 
" "$@"' <place here your unprovilegd username> -- srv/<here the path of your django project>/bin/django celeryd -BE -l info end script

into:

进入:

[Unit] 
Description=My celery worker 

[Service]
WorkingDirectory=/srv/my-project-path
User=buildout
Group=buildout
Restart=on-failure
RestartSec=20 5
ExecStart=/srv/my-project/bin/django celeryd -BE

[Install]
WantedBy=multi-user.target
Alias=myservice.service

and this works, with all the credits to the answer to this question: How to write an Ubuntu Upstart job for Celery (django-celery) in a virtualenv

这是有效的,所有学分都归功于这个问题的答案: How to write an Ubuntu Upstart job for Celery (django-celery) in a virtualenv

Edit 5 sept 2013

2013 年 9 月 5 日编辑

There is one small thing left: I have to do ctrl-c after the start command in the console (and do a status check after this one): In case somebody knows this: leave in the command, and I can update this answer...

还剩下一件小事:我必须在控制台中的 start 命令之后执行 ctrl-c(并在此之后执行状态检查):如果有人知道这一点:留在命令中,我可以更新此答案。 ..

回答by Marcin

I generally use supervisor(plus django-supervisor)for this purpose. That way, you don't need to figure out how to daemonize each process in your application (of which you have at least a webserver hosting django, plus celery, plus realistically whatever other middleware you use to support both of those). Supervisor knows how to run itself as a daemon, and all your other processes run as children of supervisor.

为此,我通常使用主管(加上django-supervisor)。这样,您就不需要弄清楚如何在您的应用程序中守护每个进程(其中至少有一个托管 django 的网络服务器,加上 celery,实际上还有您用来支持这两者的任何其他中间件)。Supervisor 知道如何将自己作为守护进程运行,而您的所有其他进程都作为 supervisor 的子进程运行。

回答by Aameer

As Marcin has explained in his answer that supervisor is usually what people end up using but if you are looking for something which could work with python3 and can't wait for supervisor's version 4 which I think will have the support for python3 then you can go with circus. After installing it, you just need to have a circus.ini file which will have all the processes which you want to daemonize and then run that sample circus.ini may look like:

正如 Marcin 在他的回答中解释的那样,主管通常是人们最终使用的,但是如果您正在寻找可以与 python3 一起使用的东西并且等不及我认为将支持 python3 的主管版本 4,那么您可以去与马戏团。安装后,您只需要一个 circus.ini 文件,该文件将包含您想要守护的所有进程,然后运行该示例 circus.ini 可能如下所示:

##代码##

if you want some more details I have a post related to the same here. Hope it saves someone some time. Thanks

如果你想了解更多细节,我在这里有一篇与相关的帖子。希望它可以节省一些时间。谢谢

回答by michel.iamit

Note: in ubuntu 16.04 my anser with the .conf file is not working anymore.

注意:在 ubuntu 16.04 中,我的 .conf 文件的分析器不再工作。

I created a .service file and put this in /etc/systemd/system/

我创建了一个 .service 文件并将其放在 /etc/systemd/system/

i can use

我可以用

sudo service myservice status

须藤服务 myservice 状态

sudo service myservice start

须藤服务 myservice 启动

sudo service myservice stop

须藤服务 myservice 停止

as commands

作为命令

e.g. this file:

例如这个文件:

myservice.service:

myservice.service:

##代码##

note i use buildout, so in setad of bin/django most users need to use the path to python and use mange.py in stead.

注意我使用 buildout,所以在 bin/django 的 setad 中,大多数用户需要使用 python 的路径并使用 mange.py 代替。

base upon: http://minecraft.gamepedia.com/Tutorials/Ubuntu_startup_script(see the with systemd section)

基于:http: //minecraft.gamepedia.com/Tutorials/Ubuntu_startup_script(参见与 systemd 部分)