Help with background task

Greetings,
In urls i have

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

python3 manage.py process_tasks

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

Some comments here:

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

But what do you mean by:

???

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?)

1 Like

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.

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.

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.

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? :open_mouth:
Thank you

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.

Got it
Thank you very much :upside_down_face: