简体   繁体   English

如何从使用uWSGI异步执行的任务记录stderr

[英]How to log stderr from task executed asynchronously using uWSGI

In my Flask app I allocate a ThreadPool before the first request and use the threads allocated here to execute tasks asynchronously 在我的Flask应用中,我在第一个请求之前分配了一个ThreadPool,并使用此处分配的线程异步执行任务

from app import app
from multiprocessing.pool import ThreadPool


@app.before_first_request
def initialize():
    app.pool = ThreadPool(10)

Sometime later... 一段时间之后...

app.pool.apply_async(exporter, args=(domains))

I am using a uWSGI backend for my webserver and this works fine except my uwsgi log in /var/log/uwsgi/%n.log only logs the stdout from threads executed from this pool. 我正在为我的Web服务器使用uWSGI后端,除了我的uwsgi登录/var/log/uwsgi/%n.log只能从该池执行的线程中记录stdout之外,其他方法都可以正常工作。 I am unable to see the stderr . 我看不到stderr I was wondering if this was possible using uwsgi logging capabilities or if I have to use a pluggable logger. 我想知道使用uwsgi日志记录功能是否可能,或者是否必须使用可插入记录器。 Thanks! 谢谢!

I reached a solution by just using the error_callback argument for apply_async . 我只使用了error_callback参数就apply_async了解决方案。

I created a function: 我创建了一个函数:

def error_handler(e):
    traceback.print_exception(type(e), e, e.__traceback__)

And then execute the asynchronous task as follows: 然后执行异步任务,如下所示:

app.pool.apply_async(exporter, args=(domains), error_callback=error_hander)

This returns the stderr to the main thread after it is done executing. 执行完后,这会将stderr返回到主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM