<p dir="auto">I have a simple flask app that I want to restart if the server port is changed in a config file. I have the following code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#!/usr/bin/env python

from flask import Flask, redirect
import os
import configparser

config = configparser.ConfigParser()
config.read('portnumber.ini')

app = Flask(__name__,static_url_path='')

HTTPPort = config['MAIN'].getint('portnumber')


def touchMe():
    with open(__file__, 'a'):
        print(&quot; - setting timestamp of &quot; + __file__ )
        os.utime(__file__, None)

@app.route(&quot;/newport/&lt;newport&gt;&quot;)
def changeRoute(newport):
    HTTPPort = newport
    name = newport
    config['MAIN']['portnumber'] = newport
    with open('portnumber.ini', 'w') as configfile:
        config.write(configfile)
    touchMe()
    newurl = &quot;http://192.168.11.15:&quot; + HTTPPort + &quot;/&quot;
    print(&quot;Redirecting to &quot; + newurl)

    return redirect(newurl, code=302)


#------------------------------------------------------------
if __name__ == &quot;__main__&quot;:

    # load options from file, which change HTTPPort value
    # LoadConfig()
    while True:
        app.run(host=&quot;0.0.0.0&quot;, port=HTTPPort, debug=True, use_reloader=True)"><pre class="notranslate"><code class="notranslate">#!/usr/bin/env python

from flask import Flask, redirect
import os
import configparser

config = configparser.ConfigParser()
config.read('portnumber.ini')

app = Flask(__name__,static_url_path='')

HTTPPort = config['MAIN'].getint('portnumber')


def touchMe():
    with open(__file__, 'a'):
        print(" - setting timestamp of " + __file__ )
        os.utime(__file__, None)

@app.route("/newport/&lt;newport&gt;")
def changeRoute(newport):
    HTTPPort = newport
    name = newport
    config['MAIN']['portnumber'] = newport
    with open('portnumber.ini', 'w') as configfile:
        config.write(configfile)
    touchMe()
    newurl = "http://192.168.11.15:" + HTTPPort + "/"
    print("Redirecting to " + newurl)

    return redirect(newurl, code=302)


#------------------------------------------------------------
if __name__ == "__main__":

    # load options from file, which change HTTPPort value
    # LoadConfig()
    while True:
        app.run(host="0.0.0.0", port=HTTPPort, debug=True, use_reloader=True)
</code></pre></div>
<p dir="auto">The this code works as expected on Windows 10, however the port fails to change on Linux (or ssl_context also does not change if used). I am running Raspbian Jessie. I have also tried disabling the reloader and replacing the python process with the following call:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="os.execl(sys.executable, 'python', __file__, *sys.argv[1:])"><pre class="notranslate"><code class="notranslate">os.execl(sys.executable, 'python', __file__, *sys.argv[1:])
</code></pre></div>
<p dir="auto">This works on Windows, however on Linux, app.run() fails on reload with ""Address already in use"" error. I have reviewed the documentation and past issues however I have not found anything specific to these symptoms.</p>
<p dir="auto">I have found that, on Linux, if I use and external script to kill the python script (pkill) and reload the script it works about half of the time. When it fails it is due the "Address already in use" error.</p>
<p dir="auto">I have also tried this with Watchdog and I get the same results on Linux. I have duplicated this with python 2.7.9 and 3.6.4. I am using Flask 0.12.2</p>
<p dir="auto">Is this a known issue and if so are there any workarounds? Is there another way to pragmatically change the port and ssl_context of a running Flask app on Linux?</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Windows 10 and Raspbian Jessie</li>
<li>Python version: 2.7.9 and 3.6.4</li>
<li>Flask version: 0.12.2</li>
<li>Werkzeug version: 0.12.2</li>
</ul>
<p dir="auto">Environment:</p>
<ul dir="auto">
<li>Python version:</li>
<li>Flask version:1.1.x</li>
</ul>
<p dir="auto">In the documentation on page <a href="https://flask.palletsprojects.com/en/1.1.x/testing/" rel="nofollow">https://flask.palletsprojects.com/en/1.1.x/testing/</a> in part Testing Flask Applications, it says <em>We begin by adding a tests directory under the application root. Then create a Python file to store our tests (test_flaskr.py). When we format the filename like test</em>*.py, it will be auto-discoverable by pytest._ First of all, there is not such a file  <em>test_flaskr.py</em> in the repository that the documentation points to.</p>
<p dir="auto">One other thing, in the code snippet from the same page:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import os
import tempfile

import pytest

from flaskr import create_app

@pytest.fixture
def client():
    db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
    flaskr.app.config['TESTING'] = True

    with flaskr.app.test_client() as client:
        with flaskr.app.app_context():
            flaskr.init_db()
        yield client

    os.close(db_fd)
    os.unlink(flaskr.app.config['DATABASE'])"><pre class="notranslate"><code class="notranslate">import os
import tempfile

import pytest

from flaskr import create_app

@pytest.fixture
def client():
    db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
    flaskr.app.config['TESTING'] = True

    with flaskr.app.test_client() as client:
        with flaskr.app.app_context():
            flaskr.init_db()
        yield client

    os.close(db_fd)
    os.unlink(flaskr.app.config['DATABASE'])
</code></pre></div>
<p dir="auto">The <code class="notranslate">create_app</code> import is not used. And <code class="notranslate">flaskr</code> module does not have a variable called <code class="notranslate">app</code> as it is being created as an app factory pattern.</p>
<p dir="auto">These mistakes make it hard to understand the documentation.</p>