本文介绍了在单独的线程中启动烧瓶应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个Python应用程序,我希望看到实时统计信息。我想用 Flask 来使它易于理解。



我的问题是我的Flask服务器应该从我的python应用程序的一开始就开始,并停在最后。它应该是这样的:

$ pre $ def $ main $
我的主应用程序$ b $ watcher.flask导入应用程序
#watcher.flask定义一个应用程序,如快速启动烧瓶文档。
#参见:http://flask.pocoo.org/docs/0.10/quickstart/#quickstart
$ b $ app.run()#启动烧瓶应用程序
$ b $ ()


















multiprocessing.Process 。然后我试图使用 threading.Thread ,但看起来像Werkzeug不喜欢它:



<$ p $在$ http $ / 0.0.0.0:10079/
上运行Flask Server线程中的异常:
Traceback(最近一次调用的最后一个):
文件/ usr / lib / python2.7 / threading.py,第810行,在__bootstrap_inner
self.run()
文件/usr/lib/python2.7/threading.py,第763行,在运行
self .__ target(* self .__ args,** self .__ kwargs)
文件... / develop-eggs / watcher.flask / src / watcher / flask / __ init__.py,第14行,在_run
app.run(host = HOSTNAME,port = PORT,debug = DEBUG)
文件... / eggs / Flask-0.10.1-py2.7.egg / flask /app.py,第772行,运行
run_simple(host,port,self,** options)
文件... / eggs / Werkzeug-0.7-py2.7.egg / werkzeug /serving.py,第609行,在run_simple
run_with_reloader(inner,extra_files,reloader_interval)
文件... / eggs / Werkzeug-0.7-py2.7.egg / werkzeug / serving.py ,第524行,在run_with_reloader
中signal.signal(signal.SIGTERM,lambda * args:sys.exit(0))
ValueError:signal只能在主线程中运行

如何在主线程中不运行烧瓶?

在调试模式下重新运行 Flask ,这会启用重新加载器(当代码改变时重新加载Flask服务器)。

Flask可以在单独的线程中正常运行,但是reloader期望在主线程中运行。






为了解决您的问题,您应该禁用调试( app.debug = False ),或者禁用重载程序( app.use_reloader =假)。
$ b 这些也可以作为参数传递给 app.run app.run debug = True,use_reloader = False)


I'm currently developping a Python application on which I want to see real-time statistics. I wanted to use Flask in order to make it easy to use and to understand.

My issue is that my Flask server should start at the very beginning of my python application and stop at the very end. It should look like that :

def main():
    """ My main application """
    from watcher.flask import app
    # watcher.flask define an app as in the Quickstart flask documentation.
    # See: http://flask.pocoo.org/docs/0.10/quickstart/#quickstart

    app.run() # Starting the flask application

    do_my_stuff()

    app.stop() # Undefined, for the idea

Because i need my application context (for the statistics), I can't use a multiprocessing.Process. Then I was trying to use a threading.Thread but it looks like Werkzeug doesn't like it :

 * Running on http://0.0.0.0:10079/
Exception in thread Flask Server:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File ".../develop-eggs/watcher.flask/src/watcher/flask/__init__.py", line 14, in _run
    app.run(host=HOSTNAME, port=PORT, debug=DEBUG)
  File ".../eggs/Flask-0.10.1-py2.7.egg/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File ".../eggs/Werkzeug-0.7-py2.7.egg/werkzeug/serving.py", line 609, in run_simple
    run_with_reloader(inner, extra_files, reloader_interval)
  File ".../eggs/Werkzeug-0.7-py2.7.egg/werkzeug/serving.py", line 524, in run_with_reloader
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
ValueError: signal only works in main thread

How can I do that without running flask in the main thread ?

解决方案

You're running Flask in debug mode, which enables the reloader (reloads the Flask server when your code changes).

Flask can run just fine in a separate thread, but the reloader expects to run in the main thread.


To solve your issue, you should either disable debug (app.debug = False), or disable the reloader (app.use_reloader=False).

Those can also be passed as arguments to app.run: app.run(debug=True, use_reloader=False).

这篇关于在单独的线程中启动烧瓶应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:13