Workshopping a way to do background callbacks + state

Hi everyone medium time plotlyier first time poster :smiling_face_with_three_hearts:

I’m 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’s 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’t realize flask sessions used cookies! Thanks for the info. the network roundtrip is actually exactly what i’m trying to avoid and is the reason I’m not using a dash Store()

So the information I’m trying to access from the request is… I’m not sure, it’s 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’ll try that!

1 Like