-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow dominate to work in async contexts #187
Conversation
Using ContextVars to allow dominate to work within async contexts. Added unit tests to ensure code works as expected.
My apologies for jumping in here! The proposed changes seem reasonable and simple enough. Perhaps Also it would be good to take the opportunity to use something with less chances of collisions than https://stackoverflow.com/questions/47601592/safest-way-to-generate-a-unique-hash-in-python |
Hi @apalala - thanks for your reply! Although I can't imagine a situation where we get an accidental collision with My understanding was that a region of the With respect to your last point on |
HI @tlonny! Any UUID is good because it will resolve race conditions in both async and threading. Using Using the tuple is correct, but using a sha1 or sha3 is more efficient in comparisons. My own preference would be to use the tuple because it's correct, it's good enough, and it makes any upcoming debugging easier. |
Great, So action items for me:
I'll get these added to the PR v. soon Thanks for the feedback mate! |
- Added .venv and .envrc to .gitignore (I use direnv and venv to keep my python environments isolated - I hope this is okay!) - Removed print statements I left in dom_tag during debugging - Replaced global incrementing int with UUID for contextvar ID generation - this zeroes the risk of race-hazards/collisions - _get_thread_context now returns a tuple vs. a hash of a tuple. Functionally not much changes - the underlying dictionary will still use the same hashing function but the only difference is that _if_ there is a collision, the dictionary will still be able to return the correct element
@apalala - Changes implemented :-) |
@Knio - are we good to merge this bad boy? Happy Xmas BTW |
Hi everyone,
This PR uses Context vars to help distinguish between different async contexts. This prevents two concurrent document edits running in an event loop from bleeding into each other.
With this fix, I hope dominate can be safely used with async web frameworks like sanic etc.