Monday, May 30, 2016

Django trick: keep "runserver" from crashing on Python syntax error

When developing Django, the "runserver" command is convenient. It runs our appserver, and reloads itself when we change our app's source code.  We can have a rapid "edit stuff then see what happened" cycle.

However, runserver it has an annoying habit. If we're typing so fast that we add a Python syntax error into our code, the  command will crash. We expect that when we fix the syntax error, we can see our results, but it doesn't work. The appserver has crashed, and is no more.
 

The following workaround works wonders. If we add a syntax error, "runserver" will crash, and this loop will wait for a moment then re-run it. We can now type as fast as possible all the time, and Django will always show us what we want to see. Or, what we give it, which sometimes is enough ;)



while true; do ./manage.py runserver; sleep 15; done

No comments:

Post a Comment