mwjackson
mwjackson

Reputation: 5441

Flask - Logging after response flushed

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?

Upvotes: 3

Views: 1263

Answers (1)

Doobeh
Doobeh

Reputation: 9440

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.

Upvotes: 2

Related Questions