Is next_element_id assignment unique and thread/async-safe? #2752
Closed
petergaultney
started this conversation in
General
Replies: 1 comment
-
I'm realizing now that since this is per-client, all you need is for a given request (with presumably unique client) to only ever operate in a single thread/greenlet at a time. And that seems quite likely to be the case. I will close this. :) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
(looking at https://github.com/zauberzeug/nicegui/blob/main/nicegui/element.py#L76-L77 )
I'm wondering if there are any edge cases where two elements could end up with the same id. Python does not guarantee atomicity of the
+=
operation, nor would it prevent multiple elements from being constructed and selecting the samenext_element_id
in separate threads.I'm not actually sure how this works in
asyncio
land, and maybenicegui
is leaning on some guarantees there.I'm also not sure how big a deal it would be if ids got shared - but my guess would be that it would cause problems of some kind.
In any case, if it were determined that it would be better to use a fully atomic counter, there are various libraries that can provide lock-free ones, or a
threading.Lock
could be used. Or there might be other cleverer strategies that I'm not thinking of.Beta Was this translation helpful? Give feedback.
All reactions