views:

26

answers:

2

I got a custom command in my reusable django app which I want to kick off a deamonized 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 spefici 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 djangos reusable app pattern.

Any hints or thoughts?

+1  A: 

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.

Daniel Roseman
A: 

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

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

Alexander Artemenko
Hmmm ... could you give me a simple wxample on how to do this? No experience using threads whatsoever. I plan to start at least two more services ( a ftpserver and mounting of a virtual filesystem), so if I could use threads to manager them all it would be nice.
Weholt
The basic example here. import threading def worker(): # do some useful job here threading.Thread(target = worker).start()Read the documentation on `threading` module.
Alexander Artemenko
Fucking formatter! Sorry. Just read the docs.
Alexander Artemenko