Bottle web framework - How to stop?

前端 未结 11 1256
不知归路
不知归路 2020-12-14 06:55

When starting a bottle webserver without a thread or a subprocess, there\'s no problem. To exit the bottle app -> CTRL + c.

In a thread, ho

11条回答
  •  自闭症患者
    2020-12-14 07:42

    For the default (WSGIRef) server, this is what I do (actually it is a cleaner approach of Vikram Pudi's suggestion):

    from bottle import Bottle, ServerAdapter
    
    class MyWSGIRefServer(ServerAdapter):
        server = None
    
        def run(self, handler):
            from wsgiref.simple_server import make_server, WSGIRequestHandler
            if self.quiet:
                class QuietHandler(WSGIRequestHandler):
                    def log_request(*args, **kw): pass
                self.options['handler_class'] = QuietHandler
            self.server = make_server(self.host, self.port, handler, **self.options)
            self.server.serve_forever()
    
        def stop(self):
            # self.server.server_close() <--- alternative but causes bad fd exception
            self.server.shutdown()
    
    app = Bottle()
    
    @app.route('/')
    def index():
        return 'Hello world'
    
    @app.route('/stop')  # not working from here, it has to come from another thread
    def stopit():
        server.stop()  
    
    server = MyWSGIRefServer(port=80)
    try:
        app.run(server=server)
    except:
        print('Bye')
    

    When I want to stop the bottle application, from another thread, I do the following:

    server.stop()
    

提交回复
热议问题