在线程内使用Flask current_app.logger [英] Use Flask current_app.logger inside threading

查看:99
本文介绍了在线程内使用Flask current_app.logger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 current_app.logger ,当我尝试登录线程内部时,它说在应用程序上下文之外工作".如何从线程中运行的方法记录消息?

I am using current_app.logger and when I tried to log inside thread it says "working outside of application context". How do I log a message from a method running in a thread?

def background():
    current_app.logger.debug('logged from thread')

@app.route('/')
def index():
    Thread(target=background).start()
    return 'Hello, World!'

线程Thread-16中的

Exception in thread Thread-16:
Traceback (most recent call last):
  File "/usr/lib64/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/sapam/demo.py", line 57, in background
    current_app.logger.critical('test')
  File "/home/sapam/.virtualenvs/demo/lib/python3.5/site-packages/werkzeug/local.py", line 343, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/sapam/.virtualenvs/demo/lib/python3.5/site-packages/werkzeug/local.py", line 302, in _get_current_object
    return self.__local()
  File "/home/sapam/.virtualenvs/demo/lib/python3.5/site-packages/flask/globals.py", line 51, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in a way.  To solve
this set up an application context with app.app_context().  See the
documentation for more information.

127.0.0.1 - - [13/Sep/2016 12:28:24] "GET / HTTP/1.1" 200 -

推荐答案

您以标准方式使用标准 logging 模块:获取当前模块的记录器并记录一条消息./p>

You use the standard logging module in the standard way: get the logger for the current module and log a message with it.

def background():
    logging.getLogger(__name__).debug('logged from thread')


app.logger 主要用于内部Flask日志记录,或者至少用于应用程序上下文内的日志记录.如果您处于线程中,那么您将不再处于同一应用程序上下文中.


app.logger is mostly meant for internal Flask logging, or at least logging within an app context. If you're in a thread, you're no longer in the same app context.

您可以将 current_app._get_current_object()传递给线程,并使用它代替 current_app .或者,您可以将 Thread 子类化以进行类似的操作.

You can pass current_app._get_current_object() to the thread and use that instead of current_app. Or you can subclass Thread to do something similar.

def background(app):
    app.logger.debug('logged from thread')

@app.route('/')
def index():
    Thread(target=background, kwargs={'app': current_app._get_current_object()}).start()
    return 'Hello, World!'

class FlaskThread(Thread):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.app = current_app._get_current_object()

    def run(self):
        with self.app.app_context():
            super().run()

def background():
    current_app.logger.debug('logged from thread')

@app.route('/')
def index():
    FlaskThread(target=background).start()
    return 'Hello, World!'

用于多处理的工作原理相同.

The same works for multiprocessing.

这篇关于在线程内使用Flask current_app.logger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆