D
D
Denis Karimov2017-05-19 13:54:29
Django
Denis Karimov, 2017-05-19 13:54:29

Django: How to execute a long running function after returning a response to the user?

Standard view function:

def view(request, id):
    article = get_object_or_404(Articles, id=id)
    return render(request, 'view.html', {'article': article})

And here we need to insert a function that will record visit statistics. It will crawl into the database, check for the existence of a record of this post and this user, and if there is none, it will create a new record.
def view(request, id):
    article = get_object_or_404(Articles, id=id)
    save_stat(request, article.id)
    return render(request, 'view.html', {'article': article})

But after some time, this function will be very long, and the user will have to wait until Django writes statistics about him, and only after that he will receive his answer.
Is it possible to first give the user a response, then, without his presence, save information about him?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2017-05-19
@trixden

Logically, you just need to execute an internal HTTP request and not wait for a response.
Of these solutions, there is Celery

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question