Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - Logging after response flushed

Tags:

python

flask

I know about app.after_request and app.teardown_request, but is there any way I can run a logging command after the response has been flushed to the client.. ie. in a way that doesn't impact the client's performance?

like image 455
mwjackson Avatar asked Mar 24 '26 00:03

mwjackson


1 Answers

I was tempted to say the request_finished signal would work, but testing it out now, it does wait until the listener is finished before returning to the user.

So I think that leaves you with implementing a task queue- Flask has some documentation on getting Celery based background tasks working. So when you reach your slow logging command, you'd instead just add the task to the Celery queue, finish your response to the user, then let a Celery worker take care of the task as it's able.

like image 185
Doobeh Avatar answered Mar 26 '26 12:03

Doobeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!