5

I'm calling a management command from a view as follows:

from django.http import JsonResponse
from django.core.management import call_command

def index(request):
    call_command('mymanagementcommand')
    response = {'result': 'success',
                'message': 'thank you, come again'}
    return JsonResponse(response)

I don't want to wait for my management command to finish before continuing through the view and returning the response. In this case, "success" just means that the command has been called and is unconcerned whether that command ran successfully.

Is there a djangoy way for me to just fire it off in the background and not wait for it?

Thanks!

user3449833
  • 779
  • 2
  • 10
  • 28

1 Answers1

0

Add the call_command to a separate python thread.

import threading

class CustomThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        call_command("mymanagementcommand")

def index(request):
   CustomThread().start()

Farooq
  • 31
  • 1