【问题标题】:Start Python web server and return control to app启动 Python Web 服务器并将控制权返回给应用程序
【发布时间】:2015-08-06 06:45:58
【问题描述】:

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

【问题讨论】:

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

标签: python flask bottle


【解决方案1】:

您可以从新线程开始。您应该设置守护程序标志,以便 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()

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    相关资源
    最近更新 更多