# Help with background task

**URL:** <https://forum.djangoproject.com/t/help-with-background-task/9930>\
**Category:** Using Django\
**Created:** [October 1, 2021, 5:48pm UTC](https://forum.djangoproject.com/t/help-with-background-task/9930 "2021-10-01T17:48:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ukrolelo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ukrolelo/32/4193_2.png) [@ukrolelo](https://forum.djangoproject.com/u/ukrolelo)\
**Post date:** [October 1, 2021, 5:48pm UTC](https://forum.djangoproject.com/t/help-with-background-task/9930/1 "2021-10-01T17:48:08Z")

</div>

Greetings,  
In urls i have

```auto
from background_task import background
@background(schedule=1)
def runonce():
    print("here1")
    time.sleep(10)
    print("here2")

print("now")
#comment to do a migration
runonce(repeat_until=None)

```

Only “now” is printed. No errors on makemigrations ,migrate or runserver.  
I need to run a seperate function on startup until the app is terminated.  
I want continuously parse a file the whole time the django app is running.

**UPDATE 1:**  
When i run

```auto
python3 manage.py process_tasks

```

The function is executed. But i want to be executed when i do runserver

Some comments here:

> <https://stackoverflow.com/questions/41205607/how-to-activate-the-process-queue-in-django-background-tasks>

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 2, 2021, 2:29am UTC](https://forum.djangoproject.com/t/help-with-background-task/9930/2 "2021-10-02T02:29:09Z")

</div>

Couple different things here.

You don’t run long-running tasks from within runserver. You run them externally, such as with a [custom django-admin command](https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/#module-django.core.management)

But what do you mean by:

> [@ukrolelo](#):
>
> I want continuously parse a file the whole time the django app

???

Once you’ve parsed the file, you’re done parsing it. What is your overall objective with this? (What are you doing that is making you think this is a viable solution?)

---

<div class="post-metadata">

**Author:** ![ukrolelo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ukrolelo/32/4193_2.png) [@ukrolelo](https://forum.djangoproject.com/u/ukrolelo)\
**Post date:** [October 2, 2021, 8:56am UTC](https://forum.djangoproject.com/t/help-with-background-task/9930/3 "2021-10-02T08:56:46Z")

</div>

Hi!  
Thank you for the link on custom command, will check it up.  
The thing is that on start of django app i want a script to run as a service and parse logs constantly and live update table on page through ajax. All working except the script that would start on django app start.But if i turn off the app i want the script to stop been active.(dont want to use lock files)  
Hope this custom command can do that.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 2, 2021, 1:08pm UTC](https://forum.djangoproject.com/t/help-with-background-task/9930/4 "2021-10-02T13:08:51Z")

</div>

What do you mean by “live update table through ajax”? Are you going to have your JavaScript request updates periodically (every couple of seconds)? Or are you looking to establish a websocket connection where the server can push the updates? (Something _completely_ different)

What do you mean by “start on django app start”? When you’re running Django on a server, the app is always running.

There are some differences in how things are handled based upon **exactly** what you’re trying to achieve.

---

<div class="post-metadata">

**Author:** ![ukrolelo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ukrolelo/32/4193_2.png) [@ukrolelo](https://forum.djangoproject.com/u/ukrolelo)\
**Post date:** [October 2, 2021, 7:18pm UTC](https://forum.djangoproject.com/t/help-with-background-task/9930/5 "2021-10-02T19:18:58Z")

</div>

> [@KenWhitesell](#):
>
> What do you mean by “live update table through ajax”? Are you going to have your JavaScript request updates periodically (every couple of seconds)? Or are you looking to establish a websocket connection where the server can push the updates? (Something _completely_ different)

i am running development with runserver and each time i want to change some thing i need to ctrl+c my running “service” script that crawl logs(and feed db) and ctrl+c django runserver.  
JS is refreshing data table with ajax and reading view template that reads from django db.  
So if the feading script would start automaticaly when runserver is started that would be easier.

> [@KenWhitesell](#):
>
> What do you mean by “start on django app start”? When you’re running Django on a server, the app is always running.

When you boot first time/or restarted by any reason, the django app starts and should signal the script to run.I would not want to mess with separation from the django app.If its possible.  
[/quote]

Would you not recommend refreshing the data, rather let server send with ws?  
I am over one hour in tutorial on WS. Do i understand correctly that i need redis to be installed?Did i understand correctly that i can loop inside the django function and feed the live data to table,for example if data changed? 😮  
Thank you

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 2, 2021, 9:29pm UTC](https://forum.djangoproject.com/t/help-with-background-task/9930/6 "2021-10-02T21:29:13Z")

</div>

> [@ukrolelo](#):
>
> Would you not recommend refreshing the data, rather let server send with ws?

I am not making any recommendations, I’m just trying to understand what you’re looking to accomplish and how you’re trying to do it.

Whether you do this as the client polling (AJAX) or as a server-side push (Websockets) to update the page is a decision for you to make, taking all the relevant factors into consideration.  
Either way, you’re going to be better off if you separate your logic of gathering data from the logs from the process of updating the pages.

---

<div class="post-metadata">

**Author:** ![ukrolelo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ukrolelo/32/4193_2.png) [@ukrolelo](https://forum.djangoproject.com/u/ukrolelo)\
**Post date:** [October 3, 2021, 7:27am UTC](https://forum.djangoproject.com/t/help-with-background-task/9930/7 "2021-10-03T07:27:06Z")

</div>

Got it  
Thank you very much 🙃
