使用 djangos manage.py 自定义命令启动守护进程服务?

发布于 2024-09-08 07:23:15 字数 479 浏览 2 评论 0原文

我在可重用的 django 应用程序中有一个自定义命令,我想启动一个守护进程服务然后返回,让服务保持运行。

我已经将我的服务实现为一个带有启动方法的简单类。当调用 start 时,它会在一个永恒循环中运行,休眠 10 秒,然后使用 django orm 检查项目 settings.py 文件中配置的数据库,检查给定文件夹中的条目。

我希望能够:

./manage.py startservice

启动我的服务并返回。然后在同一个 shell 中:

./manage.py runserver

并开始在特定数据库表中添加条目,这些条目会在 5 秒内由后台运行的服务获取并进行处理。

我研究了 celery 以寻求一种更多基于消息队列的方法,但它依赖于太多其他东西。重要的是整个事情遵循 django 的可重用应用程序模式。

有什么提示或想法吗?

I got a custom command in my reusable django app which I want to kick off a daemonized service and then return, leaving the service running.

I've implemented my service as a simple class with a start-method. When start is called it runs in an eternal loop, sleeping for 10 seconds, then using the django orm to check the database configured in the projects settings.py file, checks for entries in a given folder.

I want to be able to:

./manage.py startservice

which kicks of my service and returns. Then in the same shell:

./manage.py runserver

and start adding entries in a specific database table which within 5 seconds are picked up by the service running in the background and processed.

I've looked at celery for a more message-queue-based approach, but it relies on too much other stuff. It's important that the whole thing follows django's reusable app pattern.

Any hints or thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-09-15 07:23:15

我有一个库 django-initd 来处理这个问题:请参阅 GitHub 上的项目

Django 实际上包含一个用于进程自我守护的实用程序,在 django.utils.daemonize 中,我的库负责启动/关闭、日志记录以及与管理命令的交互。我很想知道这对您是否有帮助。

I have the beginnings of a library, django-initd, to handle this: see the project on GitHub.

Django actually includes a utility for a process to daemonize itself, in django.utils.daemonize, my library takes care of the startup/shutdown, logging, and interaction with the management command. I'd be interested to know if it's helpful for you.

冧九 2024-09-15 07:23:15

为什么要作为单独的进程启动服务?

在线程中运行,与 runserver 在同一进程中。

Why do you want to start service as a separte process?

Run in a Thread, in the same process as runserver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文