启动PythonWeb服务器并将控件返回到应用程序

启动PythonWeb服务器并将控件返回到应用程序,python,flask,bottle,Python,Flask,Bottle,我可以启动一个web服务器,比如Flask或Bottle(可能在一个新线程中?),然后将控制权返回给应用程序吗?当我启动服务器时,这两个框架的默认示例都会窃取控制权,并且不会返回。您可以在新线程中启动。您应该设置守护进程标志,以便Ctrl+C可以结束脚本 class ServerThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): app

我可以启动一个web服务器,比如Flask或Bottle(可能在一个新线程中?),然后将控制权返回给应用程序吗?当我启动服务器时,这两个框架的默认示例都会窃取控制权,并且不会返回。

您可以在新线程中启动。您应该设置守护进程标志,以便Ctrl+C可以结束脚本

class ServerThread(threading.Thread):

  def __init__(self):
    threading.Thread.__init__(self)

  def run(self):
    app.run(
      port=7777,
      host='localhost'
    )

if '__main__'==__name__:
  logging.getLogger().addHandler(logging.StreamHandler())

  thread = ServerThread()
  thread.daemon = True
  thread.start()

如果你把它放在一个线程中,它应该返回控件。