Kick off daemonized service using djangos manage.p

2019-07-29 06:43发布

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?

2条回答
何必那么认真
2楼-- · 2019-07-29 07:40

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.

查看更多
狗以群分
3楼-- · 2019-07-29 07:42

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

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

查看更多
登录 后发表回答