I have a long running script which I’m calling via AJAX. It takes about a minute to execute.
How do I set a maximum time limit for running the script like set_time_limit ?
Is this script code that is running in the context of a view? Or is it an external script being launched by a view? (If the latter, how are you running it?)
'Im using fetch()
to call the view which returns a JSON and not rendering a template.
Django doesn’t have an execution limit (as far as I’m aware). It will happily run its Python code forever if you ask it to do that.
However, you’re not serving directly from Python / Django, you generally have an application server like gunicorn etc and a reverse proxy like nginx in front of that. Nginx will definitely limit the time it waits for your application server to serve a response, which off the top of my head I think is the configuration option proxy_read_timeout
. In short, you’d set this in your configuration for gunicorn, nginx, or whatever you might be using.
However, a minute is a long time to wait for a request! Even if you show some kind of feedback to the user like a spinner, as a user I’d probably assume the request was faulty and would try it again.
Ok, but where is the code that is taking a long time being run? Is it code directly in the view, or is the view running a script as an external process?
If it’s code in the view itself, then you would look for the appropriate parameter or setting for your wsgi container (e.g. gunicorn or uwsgi). If the code is run external to your Django process, you would manage that as a timeout for that process.