Bottle web framework - How to stop?

前端 未结 11 1260
不知归路
不知归路 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:50

    This is exactly the same method than sepero's and mike's answer, but now much simpler with Bottle version 0.13+:

    from bottle import W, run, route
    from threading import Thread
    import time
    
    @route('/')
    def index():
        return 'Hello world'
    
    def shutdown():
        time.sleep(5)
        server.srv.shutdown()
    
    server = WSGIRefServer(port=80)
    Thread(target=shutdown).start()
    run(server=server)
    

    Also related: https://github.com/bottlepy/bottle/issues/1229 and https://github.com/bottlepy/bottle/issues/1230.


    Another example with a route http://localhost/stop to do the shutdown:

    from bottle import WSGIRefServer, run, route
    from threading import Thread
    
    @route('/')
    def index():
        return 'Hello world'
    
    @route('/stop')
    def stopit():
        Thread(target=shutdown).start()
    
    def shutdown():
        server.srv.shutdown()
    
    server = WSGIRefServer(port=80)
    run(server=server)
    

    PS: it requires at least Bottle 0.13dev.

提交回复
热议问题