Interrupting Flask app in iPythonNotebook cause ZMQerror

I'm trying to run a most simple demo of flask app in an iPython notebook like this.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():.
    return 'Hello World!'

if __name__ == '__main__':
    app.run(d)

The first time I run it, all is fine. Then I interrupted the cell with app.run() in it. But the next time I run it, the notebook throws back some error message like this:

An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

Then I %tbed and get the following traceback:

SystemExit                                Traceback (most recent call last)
<ipython-input-7-a59dfe133898> in <module>()
----> 1 myapp.run(debug=True)

C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\flask\app.pyc in run(self, host, port, debug, **options)
    770         options.setdefault('use_debugger', self.debug)
    771         try:
--> 772             run_simple(host, port, self, **options)
    773         finally:
    774             # reset the first request information if the development server

C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\werkzeug\serving.pyc in run_simple(hostname, port, application, use_reloader, use_debugger, use_evalex, extra_files, reloader_interval, reloader_type, threaded, processes, request_handler, static_files, passthrough_errors, ssl_context)
    688         from ._reloader import run_with_reloader
    689         run_with_reloader(inner, extra_files, reloader_interval,
--> 690                           reloader_type)
    691     else:
    692         inner()

C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\werkzeug\_reloader.pyc in run_with_reloader(main_func, extra_files, interval, reloader_type)
    248             reloader.run()
    249         else:
--> 250             sys.exit(reloader.restart_with_reloader())
    251     except KeyboardInterrupt:
    252         pass

SystemExit: 1

Not telling me much so I looked at the cmd where I started iPython and see this: Traceback (most recent call last):

  File "C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\ipy
kernel\__main__.py", line 3, in <module>
    app.launch_new_instance()
  File "C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\traitlets\config\application.py", line 588, in launch_instanceapp.initialize(argv)
  File "<decorator-gen-122>", line 2, in initialize
  File "C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\tra
itlets\config\application.py", line 74, in catch_config_error
    return method(app, *args, **kwargs)
  File "C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\ipy
kernel\kernelapp.py", line 375, in initialize
    self.init_sockets()
  File "C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\ipy
kernel\kernelapp.py", line 231, in init_sockets
    self.shell_port = self._bind_socket(self.shell_socket, self.shell_port)
  File "C:\Users\Lewis\AppData\Local\Enthought\Canopy\User\lib\site-packages\ipy
kernel\kernelapp.py", line 173, in _bind_socket
    s.bind("tcp://%s:%i" % (self.ip, port))
  File "zmq/backend/cython/socket.pyx", line 489, in zmq.backend.cython.socket.S
ocket.bind (zmq\backend\cython\socket.c:4824)
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc.
_check_rc (zmq\backend\cython\socket.c:7055)
    raise ZMQError(errno)
ZMQError: Address in use

Seems iPython notebook server didn't handle my interruption properly. But when I tried to look for the ghost process, which listens to port 5000, I got nothing. I guess a reboot may most probably fix everthing, but just wondering if there's a fix that does not require a reboot?

Answers

Set debug=False

app.run(debug=False)
Posted on by Will Haslam