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.










share|improve this question

















  • 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














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.










share|improve this question

















  • 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












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 the AppConfig.ready() hook to do that: docs.djangoproject.com/en/dev/ref/applications/…
    – Will Keeling
    Nov 10 at 21:26












  • 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







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












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.






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto