Search code examples
pythonmultithreadinghttpserver

Can't exit Asynchronous Web.py Web Server


Going off of this example, could someone please tell me why I can not kill this program with Ctrl+C:

#!/usr/bin/env python
import web
import threading
class MyWebserver(threading.Thread):
   def run (self):
      urls = ('/', 'MyWebserver')
      app = web.application(urls, globals())
      app.run()

   def POST (self):
      pass

if __name__ == '__main__':
   MyWebserver().start()

Solution

  • Launch the thread like this:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import web
    import threading
    
    from time import sleep
    
    class MyWebserver(threading.Thread):
        def run(self):
            urls = ('/', 'MyWebserver')
            app = web.application(urls, globals())
            app.run()
    
    if __name__ == '__main__':
        t = MyWebserver()
        t.daemon = True
        t.start()
        while True:
            sleep(100)