Django background thread
up vote
0
down vote
favorite
I am making a Django app. it needs to do background work (interacting with outside APIs) while the server is running, so it looks to me like I need to start a separate thread independent from all outside requests. What is the best way to go about this?
my first instinct was to simply import threading
and start another thread, but where would I place that code in the Django app? is that even the right approach? do I need to make an outside service script that communicates with the app?
my googling has only lead me to things like django-background-tasks and celery, though from my understanding those solutions are only for tasks in views that take a long time, for returning the webpage before the task is actually done. do these solutions have support for background threads? I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
I want it to be preferably self-contained inside the app, so a solution without an outside service script would be ideal. I want the app to be installable via pip.
python django python-3.x python-multithreading
add a comment |
up vote
0
down vote
favorite
I am making a Django app. it needs to do background work (interacting with outside APIs) while the server is running, so it looks to me like I need to start a separate thread independent from all outside requests. What is the best way to go about this?
my first instinct was to simply import threading
and start another thread, but where would I place that code in the Django app? is that even the right approach? do I need to make an outside service script that communicates with the app?
my googling has only lead me to things like django-background-tasks and celery, though from my understanding those solutions are only for tasks in views that take a long time, for returning the webpage before the task is actually done. do these solutions have support for background threads? I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
I want it to be preferably self-contained inside the app, so a solution without an outside service script would be ideal. I want the app to be installable via pip.
python django python-3.x python-multithreading
1
Starting a thread from the main app seems like a reasonable approach, especially since you need it to be self-contained. You should be able to use theAppConfig.ready()
hook to do that: docs.djangoproject.com/en/dev/ref/applications/…
– Will Keeling
Nov 10 at 21:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am making a Django app. it needs to do background work (interacting with outside APIs) while the server is running, so it looks to me like I need to start a separate thread independent from all outside requests. What is the best way to go about this?
my first instinct was to simply import threading
and start another thread, but where would I place that code in the Django app? is that even the right approach? do I need to make an outside service script that communicates with the app?
my googling has only lead me to things like django-background-tasks and celery, though from my understanding those solutions are only for tasks in views that take a long time, for returning the webpage before the task is actually done. do these solutions have support for background threads? I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
I want it to be preferably self-contained inside the app, so a solution without an outside service script would be ideal. I want the app to be installable via pip.
python django python-3.x python-multithreading
I am making a Django app. it needs to do background work (interacting with outside APIs) while the server is running, so it looks to me like I need to start a separate thread independent from all outside requests. What is the best way to go about this?
my first instinct was to simply import threading
and start another thread, but where would I place that code in the Django app? is that even the right approach? do I need to make an outside service script that communicates with the app?
my googling has only lead me to things like django-background-tasks and celery, though from my understanding those solutions are only for tasks in views that take a long time, for returning the webpage before the task is actually done. do these solutions have support for background threads? I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
I want it to be preferably self-contained inside the app, so a solution without an outside service script would be ideal. I want the app to be installable via pip.
python django python-3.x python-multithreading
python django python-3.x python-multithreading
asked Nov 10 at 19:35
mateuszdrwal
2616
2616
1
Starting a thread from the main app seems like a reasonable approach, especially since you need it to be self-contained. You should be able to use theAppConfig.ready()
hook to do that: docs.djangoproject.com/en/dev/ref/applications/…
– Will Keeling
Nov 10 at 21:26
add a comment |
1
Starting a thread from the main app seems like a reasonable approach, especially since you need it to be self-contained. You should be able to use theAppConfig.ready()
hook to do that: docs.djangoproject.com/en/dev/ref/applications/…
– Will Keeling
Nov 10 at 21:26
1
1
Starting a thread from the main app seems like a reasonable approach, especially since you need it to be self-contained. You should be able to use the
AppConfig.ready()
hook to do that: docs.djangoproject.com/en/dev/ref/applications/…– Will Keeling
Nov 10 at 21:26
Starting a thread from the main app seems like a reasonable approach, especially since you need it to be self-contained. You should be able to use the
AppConfig.ready()
hook to do that: docs.djangoproject.com/en/dev/ref/applications/…– Will Keeling
Nov 10 at 21:26
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
If you plan to use celery, you can use celery beat
to run the task periodically without it being triggered from a view. django-celery-beat
allows you to manage the scheduling via django-admin. You still need to run the beat scheduler and the celery worker as separate processes.
However, implementing your task using celery might complicate the setup process for users installing your application via pip because they would also need to have celery properly setup.
A simpler approach to solving this problem is by implementing the task as a django management command that you then run from the command prompt and can schedule running it as a regular cronjob. Your users will still have to be reminded to configure the cronjob after installing your package.
I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
– mateuszdrwal
Nov 10 at 20:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you plan to use celery, you can use celery beat
to run the task periodically without it being triggered from a view. django-celery-beat
allows you to manage the scheduling via django-admin. You still need to run the beat scheduler and the celery worker as separate processes.
However, implementing your task using celery might complicate the setup process for users installing your application via pip because they would also need to have celery properly setup.
A simpler approach to solving this problem is by implementing the task as a django management command that you then run from the command prompt and can schedule running it as a regular cronjob. Your users will still have to be reminded to configure the cronjob after installing your package.
I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
– mateuszdrwal
Nov 10 at 20:22
add a comment |
up vote
1
down vote
If you plan to use celery, you can use celery beat
to run the task periodically without it being triggered from a view. django-celery-beat
allows you to manage the scheduling via django-admin. You still need to run the beat scheduler and the celery worker as separate processes.
However, implementing your task using celery might complicate the setup process for users installing your application via pip because they would also need to have celery properly setup.
A simpler approach to solving this problem is by implementing the task as a django management command that you then run from the command prompt and can schedule running it as a regular cronjob. Your users will still have to be reminded to configure the cronjob after installing your package.
I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
– mateuszdrwal
Nov 10 at 20:22
add a comment |
up vote
1
down vote
up vote
1
down vote
If you plan to use celery, you can use celery beat
to run the task periodically without it being triggered from a view. django-celery-beat
allows you to manage the scheduling via django-admin. You still need to run the beat scheduler and the celery worker as separate processes.
However, implementing your task using celery might complicate the setup process for users installing your application via pip because they would also need to have celery properly setup.
A simpler approach to solving this problem is by implementing the task as a django management command that you then run from the command prompt and can schedule running it as a regular cronjob. Your users will still have to be reminded to configure the cronjob after installing your package.
If you plan to use celery, you can use celery beat
to run the task periodically without it being triggered from a view. django-celery-beat
allows you to manage the scheduling via django-admin. You still need to run the beat scheduler and the celery worker as separate processes.
However, implementing your task using celery might complicate the setup process for users installing your application via pip because they would also need to have celery properly setup.
A simpler approach to solving this problem is by implementing the task as a django management command that you then run from the command prompt and can schedule running it as a regular cronjob. Your users will still have to be reminded to configure the cronjob after installing your package.
answered Nov 10 at 19:55
AdonisN
41239
41239
I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
– mateuszdrwal
Nov 10 at 20:22
add a comment |
I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
– mateuszdrwal
Nov 10 at 20:22
I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
– mateuszdrwal
Nov 10 at 20:22
I'm not looking for running a function every x amount of time, I want it to run forever from the moment Django starts.
– mateuszdrwal
Nov 10 at 20:22
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242699%2fdjango-background-thread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Starting a thread from the main app seems like a reasonable approach, especially since you need it to be self-contained. You should be able to use the
AppConfig.ready()
hook to do that: docs.djangoproject.com/en/dev/ref/applications/…– Will Keeling
Nov 10 at 21:26