Kick off daemonized service using djangos manage.py custom command?

孤街浪徒 提交于 2020-01-15 09:27:28

问题


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?


回答1:


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.




回答2:


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

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



来源:https://stackoverflow.com/questions/3201799/kick-off-daemonized-service-using-djangos-manage-py-custom-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!