Workshopping a way to do background callbacks + state

Hi everyone medium time plotlyier first time poster :smiling_face_with_three_hearts:

I鈥檓 running into an issue where background callbacks seem to not able to access the flask state and so the functions run by celery result in RuntimeError: Working outside of request context when you try and output to state outputs.

This seems like a known issue [BUG] Flask contexts not available inside background callback 路 Issue #2235 路 plotly/dash 路 GitHub but just wondering if anyone has any advice on how I could work around it.

Was thinking of maybe using flask sessions to store my python objects for callbacks, and thereby not have to output state from callbacks, but not sure if this is something that鈥檚 possible with dash

Thanks!

Hello @spookyuser,

Welcome to the community (officially)!

Flask session cookies are nice. But also have size limits, and make the transfer for round trip network calls that much heavier.

If you can use it, then go ahead, storage is 4096 bytes.

The reason background callbacks will no longer capture the users info is because the request element is removed from the equation. This is the same reason current_user info from layout spin up in pages fail.

What information are you trying to access exactly from the request?

Thanks so much for the reply and welcome :heart:

Flask session cookies are nice. But also have size limits, and make the transfer for round trip network calls that much heavier.

oooo I didn鈥檛 realize flask sessions used cookies! Thanks for the info. the network roundtrip is actually exactly what i鈥檓 trying to avoid and is the reason I鈥檓 not using a dash Store()

So the information I鈥檓 trying to access from the request is鈥 I鈥檓 not sure, it鈥檚 something that dash_extensions requires. My workaround for passing large objects until now has been to use the awesome dash_extensions ServerSideOutput function - but as soon as I switched over to doing celery background=True callbacks it stopped working I detailed my sad journey to realising this was never gonna work here: Support background callback using Redis & Celery 路 Issue #207 路 emilhe/dash-extensions 路 GitHub :cry:

Ok.

Just for kicks, in your function call, have you done this?

def mycallback(arg1,arg2,arg3,request=flask.request)

Nope, I鈥檒l try that!