I would like to hold running threads in my Django application. Since I cannot do so in the model or in the session, I thought of holding them in a singleton. I’ve been checking this out for a while and haven’t really found a good how-to for this.
Does anyone know how to create a thread-safe singleton in python?
EDIT:
More specifically what I wand to do is I want to implement some kind of “anytime algorithm”, i.e. when a user presses a button, a response returned and a new computation begins (a new thread). I want this thread to run until the user presses the button again, and then my app will return the best solution it managed to find. to do that, i need to save somewhere the thread object – i thought of storing them in the session, what apparently i cannot do.
The bottom line is – i have a FAT computation i want to do on the server side, in different threads, while the user is using my site.
Unless you have a very good reason – you should execute the long running threads in a different process altogether, and use Celery to execute them:
Celery guide for djangonauts: http://django-celery.readthedocs.org/en/latest/getting-started/first-steps-with-django.html
For singletons and sharing data between tasks/threads, again, unless you have a good reason, you should use the db layer (aka, models) with some caution regarding db locks and refreshing stale data.
Update: regarding your use case, define a
Computationmodel, with astatusfield. When a user starts a computation, an instance is created, and a task will start to run. The task will monitor thestatusfield (check db once in a while). When a user clicks the button again, a view will change the status touser requested to stop, causing the task to terminate.