My interface must give users an option to start a long web-scraping operation, which could take from some minutes to a couple of hours. As this operation runs, it will persist data to the database. It's coded in this fashion:
def my_view(request):
batch = BatchTable.objects.create(user=request.user) # Batch model registers who started the operation
try:
long_running_operation(batch) # How can THIS be made to run after response being sent?
except Exception as ex:
batch.fail = str(ex)
batch.failed = datetime.now()
batch.save()
return JsonResponse({'success': 'True', 'message': 'Batch started'})
def long_running_operation(batch):
list_response = requests.get('https://agiven.web.service/rest/api/objects')
object_list = list_response.json()
batch.total = len(object_list)
batch.save()
for object_entry in list_response.json():
object_response = requests.get(f'https://agiven.web.service/rest/api/objects/{object_entry.id}')
object_dict = object_response.json()
object_dict['batch'] = batch # to link the entry to the batch in which it was read and created
ObjectTable.objects.create(**object_dict) # to persist object data in the bd
batch.progress += 1
batch.save()
batch.finalized = datetime.now()
batch.save()
my_view()
is hit by an AJAX request, but of course this request will hang until long_running_operation()
has finished.
Is there some way of sending the response first from my_view()
and continuing execution by only then calling long_running_operation()
?