如何创建 Celery Windows 服务?
我正在尝试创建一个 Windows 服务来启动 Celery。我遇到过一篇文章,它使用 任务计划程序。然而,它似乎启动了许多 celery 实例并不断消耗内存,直到机器死掉。有什么办法可以将其作为 Windows 服务启动吗?
I'm trying to create a Windows Service to launch Celery. I have come across an article that does it using Task Scheduler. However it seems to launch numerous celery instances and keeps eating up memory till the machine dies. Is there any way to launch it as a Windows service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我从另一个网站得到了答案。 Celeryd(Celery 的守护进程服务)作为粘贴应用程序运行,搜索“Paster Windows Service”引导我 此处。它描述了如何将 Pylons 应用程序作为 Windows 服务运行。作为 Paster 框架和托管 Python Web 服务的新手,我一开始并没有想到要检查它。但该解决方案适用于 Celery,只是在脚本中略有更改。
我修改了脚本,以便更轻松地修改 Celery 设置。基本的更改是:
INI 文件设置 (celeryd.ini):
用于创建 Windows 服务 (CeleryService.py) 的 Python 脚本:
要安装服务,请运行
python CeleryService.py install
,然后运行 python CeleryService.py start 启动服务。 注意:这些命令应在具有管理员权限的命令行中运行。
如果需要删除服务,请运行
python CeleryService.py remove
。我试图托管 Celery 作为增强 RhodeCode 安装的一部分。这个解决方案似乎有效。希望这会对某人有所帮助。
I got the answer from another website. Celeryd (daemon service for Celery) runs as a paster application, searching for 'Paster Windows Service' lead me here. It describes how to run a Pylons application as a Windows Service. Being new to paster framework and hosting python web services, it didn't cross my mind to check it at first. But that solution works for Celery with a slight change here and there in the script.
I've modified the script to make it easier for modifying Celery settings. The essential changes are:
INI file settings (celeryd.ini):
Python script to create Windows Service (CeleryService.py):
To install the service run
python CeleryService.py install
and thenpython CeleryService.py start
to start the service. NOTE: These commands should be run in command-line with administrator rights.If the service needs to be removed, run
python CeleryService.py remove
.I was trying to host Celery as part of enhancing my RhodeCode installation. This solution seems to work. Hope this will help someone.
接受的答案不适用于使用 Django 应用程序运行 celery。但它启发我想出了一个使用 Django 将 celery 作为 Windows 服务运行的解决方案。请注意,以下内容仅适用于 Django 项目。经过一些修改,它可以与其他应用程序一起使用。
以下讨论假设已安装 Python >= 3.6 和 RabbitMQ,并且
rabbitmq-server
正在localhost
上运行。在 Django 项目的顶级文件夹中创建一个文件 celery_service.py (或任何您喜欢的文件),与 manage.py 相同级别,并包含以下内容:
在运行脚本之前,您需要
可以选择创建一个 python 虚拟文件环境,例如“venvcelery”。
安装以下要求:
django>=2.0.0
sqlalchemy>=1.0.14
芹菜>=4.3.0,<5.0
pywin32>=227
eventlet>=0.25
修复 pywin32
pywintypes36.dll
位置。 ref在 celery_service.py 中正确设置 PYTHONSCRIPTPATH 和 PROJECTDIR
PYTHONSCRIPTPATH 是通常是Python安装路径下的“Scripts”文件夹或当前虚拟环境
PROJECTDIR是 Django 项目的目录名称。
是manage.py的同级目录,不是父目录。
现在您可以安装/启动/停止/删除服务:
我创建了一个演示 Django 项目,其中 celery 作为 Windows 服务运行:
https://github.com/azalea/django_celery_windows_service
如果您对运行示例感兴趣。
注意:这是假设 Python >= 3.6、Django 2.2 和 Celery 4 的更新版本
。可以在编辑历史记录中查看包含 Python 2.7、Django 1.6 和 Celery 3 的旧版本。
The accepted answer does not apply for running celery with a Django application. But it inspired me to come up with a solution for running celery as a Windows service with Django. Note that the following is for Django projects only. It may work with other applications with some modifications.
The following discussion assumes Python >= 3.6 and RabbitMQ are already installed, and
rabbitmq-server
is running onlocalhost
.Create a file celery_service.py (or whatever you like) inside your Django project's top level folder, same level as manage.py, with the following content:
Before the script can be run, you need to
Optionally create a python virtual environment e.g. 'venvcelery'.
Install the following requirements:
django>=2.0.0
sqlalchemy>=1.0.14
celery>=4.3.0,<5.0
pywin32>=227
eventlet>=0.25
Fix pywin32
pywintypes36.dll
location. refCorrectly set PYTHONSCRIPTPATH and PROJECTDIR in celery_service.py
PYTHONSCRIPTPATH is usually the "Scripts" folder under your python's installation path or current virtual environment
PROJECTDIR is the directory name of the Django project.
It is the directory at the same level of manage.py, not the parent directory.
Now you can install / start / stop / remove the service with:
I created a demo Django project with celery running as a Windows service:
https://github.com/azalea/django_celery_windows_service
In case you are interested in a running example.
Note: this is an updated version assuming Python >= 3.6, Django 2.2 and Celery 4.
An older version with Python 2.7, Django 1.6 and Celery 3 can be viewed in edit history.
@azalea 的回答对我帮助很大,但我想在这里强调的一件事是,服务(celery_service.py)需要使用您的用户/密码进行安装,否则,当您运行 subprocess.Popen(args )在 SvcDoRun() 函数中,不会发生任何事情,因为会出现权限问题。要设置用户/密码,您可以选择以下两种方法之一:
使用命令行:
转到计算机管理(本地)>服务和应用>服务,找到您的服务器(在@azalea的示例中,它是“Celery Distributed Task Queue Service”),右键单击打开“属性”页面,在“登录”选项卡中输入“此帐户”
@azalea 's answer helped me a lot, but one thing I like to highlight here is, the service (celery_service.py) needs to be installed with your user/password, otherwise, when you run
subprocess.Popen(args) in SvcDoRun()
function, nothing will happen as there will be a permission issue. To set the user/password, you can choose one of two methods:Using command line:
Go to Computer Management(local) > Services and Applications > Services, find your server (in @azalea's example, it is "Celery Distributed Task Queue Service"), and right click to open Properties page, input "This account" in Log On tab
这里有一个很好的项目,但没有成功使用它:
链接到 django-windows-tools 的 GitHub。
它在最后一个命令行给了我一个超时。没有足够的时间去寻找原因。
该包允许在 IIS 上设置 Django 项目的 FastCGI、Celery 和静态文件。
A good project here but didn't succeed to use it :
Link to the GitHub of the django-windows-tools.
It gave me a timeout at the last command line. Doesn't have enough time to search why.
The package allow settings FastCGI, Celery and Static files of a Django project on IIS.
感谢 Azalea,这让我走上了能够在 Windows 上使用 Celery 4 创建 2 个 Windows 服务的道路。
一个能够启动/停止多个工人 T
其次,能够启动/停止beat服务并使用Celery 4整理pid。
唯一需要注意的是,我没有解决方案,你不能重新启动worker,因为你需要确保多个进程的生成在开始备份之前停止。
Workers.py:
Beatservice.py:
Thanks to Azalea as this led me to the path in being able to create 2 windows services with Celery 4 on Windows.
One to be able to start/stop multiple workers T
Second to be able to start/stop the beat service and tidy up the pid using Celery 4.
Only caveat in this that I do not have a solution for is you can not restart the workers as you need to ensure the spawned processes for multiple are stopped before starting back up.
Workers.py:
Beatservice.py: