Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: run admin command in background and notify html when finished

I have set up an admin command in django:

from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
    """ Command to parse logs that can be called from django """
    def handle(self, *args, **options):
        parse_logs()

I can call either from shell or python with call_command. When I call it from the view this command stops all other requests to the app from working until it is finished.

I would like to be able to run this command in the background, maybe using a separate Thread, and displaying the results of the command on the webpage without having to reload the page. For example:

  1. Call command from webpage
  2. Continue doing other stuff in the webpage
  3. Once command is finished, display a notification on the webpage
  4. Never reload the webpage
like image 513
liarspocker Avatar asked May 08 '26 13:05

liarspocker


1 Answers

You could use the subprocess module (Popen) to execute the management command as a separate process (thus liberating your view from having to wait until the command is done).

The process could set some kind of flag when it's done. Then you would poll the server for results until they are available, i.e. the flag has been set

The process would also have to be able to communicate results once they're available.

like image 77
ryuusenshi Avatar answered May 10 '26 01:05

ryuusenshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!