user7111260
user7111260

Reputation:

Where should I start a new thread when the django server starts

I need a thread running when I start my django server, basically the thread just periodically processes some items from a database.

Where is the best place to start this thread.

Upvotes: 0

Views: 311

Answers (2)

Juho Rutila
Juho Rutila

Reputation: 2468

I think this is generally a bad idea. You shouldn't have that kind of periodic threads running in the frontend process.

I would create a management command that will do the processing. Then I would set up a cron job (or any other mechanic provided by the hosting) calling the management command. This way you divide the work to logic places and you can also test the processing much easier.

Upvotes: 2

Shariq
Shariq

Reputation: 506

You want to execute code in the top-level urls.py. That module is imported and executed once on server startup.

in your urls.py

from django.confs.urls.defaults import *
from my_app import one_time_startup

urlpatterns = ...

one_time_startup() # This is your function that you want to execute. 

Upvotes: 0

Related Questions