Skip to content
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

Add context manager for unpinning #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bardiharborow
Copy link

@bardiharborow bardiharborow commented Jun 28, 2022

Building on #56, this PR adds a UseSecondaryDB context manager which unpins within a block.

(You can just review the second commit, apparently I can't stack PRs from a fork. I will rebase if/when the first PR merges.)

This commit adds support for nested use of the UsePrimaryDB context manager. If a new instance of the context manager was created at each level we could use instance variables to store the pinning state but the default instance is generally used directly. Instead, we store a stack of pinning values locally in the thread and pop each value as we unwind the stack.
This commit adds a UseSecondaryDB context manager which unpins within a block.

def __exit__(self, type, value, tb):
if not _locals.old.pop():
previous_state = _locals.old.pop()
if previous_state:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ This introduces a subtle behaviour change. Previously, manual use of unpin_this_thread() would leak out of the context block, for example when:

  1. a thread is pinned
  2. enters the context block (_locals.old = True)
  3. is manually unpinned (with unpin_this_thread())
  4. exits the context block (not _locals.old == False so no action is taken)
  5. the thread will be unpinned when it reaches here

This new code will always restore the previous pinning state when exiting a context block. In my view this is more predictable, but I'm open to your opinion on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant