It depends upon two aspects of your requirements:
-
Are the users making the request expecting to see the results as a response to the request?
-
Does the execution of this algorithm require more time than the timeout value for your server?
If the answer to the first question is “No”, then you can do it as a background task.
If the answer to the second question is “Yes”, then you must do it as a background task.
If you are handling this synchronously in a request, then the the code could be in a view or it could be in a separate module - that’s entirely up to you. It doesn’t really matter from Django’s perspective, it’s a personal choice and how you are most comfortable doing it.
If you’re handling it asynchronously, then it must be in a separate task.