3

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
liarspocker
  • 2,434
  • 3
  • 17
  • 26
  • Use [Celery](http://docs.celeryproject.org/en/master/index.html). – Daniel Roseman May 20 '14 at 13:00
  • I am aware of celery. But it's very hard to setup in my environment (no priviledges) and it seems a bit like an overkill to execute a single task asynchronously. Can't it be done with ajax? – liarspocker May 22 '14 at 15:55

1 Answers1

3

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.

ryuusenshi
  • 1,976
  • 13
  • 15
  • 2
    subprocess.Popen did the work Thank you.. I was actually using suprocess.call which was waiting for command to finish till it redirects to the .desired page. – just10minutes Dec 17 '16 at 17:14