Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a background task continuously in Django

Tags:

python

django

I am running a server in Django,which is taking values continuously. The function used forever loop in it, when I call that function it never gets out of the loop.

My problem - I want to take values continuously from the server and use it afterwords wherever I want.

I tried threading, what I thought I could do is make a background task which keeps on feeding the database and when I want to use I can take values from it. But I dont know how to do this

    ip = "192.168.1.15"

    port = 5005

    def eeg_handler(unused_addr, args, ch1, ch2, ch3, ch4, ch5):

        a.append(ch1)

        print(a)

    from pythonosc import osc_server, dispatcher

    dispatcher = dispatcher.Dispatcher()

    dispatcher.map("/muse/eeg", eeg_handler, "EEG")

    server = osc_server.ThreadingOSCUDPServer(
        (ip, port), dispatcher)

    # print("Serving on {}".format(server.server_address))

    server.serve_forever()
like image 440
Ambuje Gupta Avatar asked Sep 18 '25 12:09

Ambuje Gupta


1 Answers

You can create a Management command

With a Management command you can acces to your database in the same way you accesss to it through Django.

Then you can schedule this command from cron or you can make this run forever because it will not block your application.

Another guide to write management command.

like image 143
Davide Pizzolato Avatar answered Sep 20 '25 01:09

Davide Pizzolato