Per message context for async socketio client app #741
Replies: 8 comments
-
I'm not sure I really understand what you want, but Flask application and request contexts are based on the contextvar objects from Python. So maybe that is what you want? |
Beta Was this translation helpful? Give feedback.
-
I had tried context vars, but what I hadn't realized was that I was on python3.6 and I had done a "pip install contextvars", with that package imported, things didn't work as expected. I moved to 3.7 and contextvars work as expected. |
Beta Was this translation helpful? Give feedback.
-
Hi @miguelgrinberg I think I don't fully understand contextvar but can I use it for each event of same sid like this? some_ctx = contextvars.ContextVar('some_ctx')
@sio.event
async def connect(sid, eviron, auth):
result = get_some_ctx(auth)
some_ctx.set(result)
@sio.event
async def disconnect(sid):
result = some_ctx.get()
# some code goes here I know there's |
Beta Was this translation helpful? Give feedback.
-
@mozartilize Each event handler runs in the context of a new async task, so no, it is not the appropriate use for what you want to do. The user sessions is what you want. |
Beta Was this translation helpful? Give feedback.
-
@miguelgrinberg thank you, how about its scale if I run on gunicorn with multiple uvicorn workers? |
Beta Was this translation helpful? Give feedback.
-
I don't understand what do you mean by scale. Using a contextvar you seemed okay storing one value per client, so how is it different with a session? |
Beta Was this translation helpful? Give feedback.
-
yeah, I'm messed up. Look like there's nothing I can do but putting more ram in it if thousands of users connect to my service :D |
Beta Was this translation helpful? Give feedback.
-
@mozartilize Well, if you need to store a lot of data for each user, then you can use a database. Or Redis. Then the only thing you need to save in memory is the key under which the data for each user is stored in your db. |
Beta Was this translation helpful? Give feedback.
-
This is a question:
I am using asyncio + socketio[client] in python 3.6. The program connects to a socketio server (hub). It is possible for my program to get multiple concurrent requests, each of which carries a context (say inside the payload of the incoming message).
eventually, I want this context to appear in JSON log (I will set up a JSON formatted that picks a dynamic context field and logs it)., so I can push it to ElasticSearch and be able to search based on the context-id (think transaction tracing).
I explored contextvars, but I see the context getting overwritten with the latest request.
Like how flask has request context, is there a way to associate context to every incoming async socketio message ? This context can be something we pull from payload or something we create on the fly (say unique request-Id). The context must be derivable from any functions that are called in the tree, like the log formatter.
Beta Was this translation helpful? Give feedback.
All reactions