`manage.py runserver` and Ctrl+C (Django)
<p>When I quit Django <code>manage.py runserver</code> with Ctrl+C, do threads running HTTP request finish properly or are they interrupted in the middle?</p>

<p><strong>TL;DR</strong> running HTTP requests are stopped when Ctrl+C is hit on the django dev server</p>

<p>I thought your question is really interesting and investigated:</p>

<p>I made a view that takes 10 seconds to execute and after that sends a response.
To test for your behaviour I stopped the development-server <code>manage.py runserver</code> using Ctrl+C and checked for the results.</p>

<p>My base test:</p>

<pre><code>class TestView ( generic.View ):
    def get ( self, request ):
        import time
        time.sleep(10)
        response = HttpResponse('Done.')
        return response
</code></pre>

<ul>
<li>Normal execute (10s runtime): Displays the msg <code>Done.</code></li>
<li>interrupted execute (Ctrl+C while the request is running): Browser error, the host cannot be reached</li>
</ul>

<p>so far everything as expected. But I played around a little bit, because Ctrl+C in python is <strong>not a full stop</strong>, but actually handled rather conveniently: As soon as Ctrl+C is hit, a <code>KeyboardInterrupt</code> aka <strong>an Exception is risen</strong> (equivalent to this):</p>

<pre><code>raise KeyboardInterrupt()
</code></pre>

<p>so in your command-line based programm you can put the following:</p>

<pre><code>try:
    some_action_that_takes_a_while()
except KeyboardInterrupt:
    print('The user stopped the programm.')
</code></pre>

<p>ported to django the new view looks like that:</p>

<pre><code>def get ( self, request ):
    import time
    slept_for = 0
    try:
        for i in range( 100 ):
            slept_for += 0.1
            time.sleep( 0.1 )
    except KeyboardInterrupt:
        pass

    response = HttpResponse( 'Slept for: ' + str( slept_for ) + 's' )
    return response
</code></pre>

<ul>
<li>Normal execute (10s runtime): Displays the msg <code>Slept for: 10s</code></li>
<li>interrupted execute (Ctrl+C while the request is running): Browser error, the host cannot be reached</li>
</ul>

<p>so no change in behaviour here. out of interest i changed one line, but the result didn't change; i used</p>

<pre><code>slept_for = 1000*1000
</code></pre>

<p>instead of </p>

<pre><code>time.sleep( 0.1 )
</code></pre>

<p>so to finally answer your question: on Ctrl+C the dev server shuts down immediately and <em>running http-requets</em> are <strong>not finished</strong>.</p>

python django signals