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

(5) Add reset() to thread local ContextVar and no-op copy_context() #2570

Merged

Conversation

antonpirker
Copy link
Member

Improves the capabilities of our threadlocal context variables, we use when no "real" context variables are available (for example in old Python versions)

  • Adds a no-op copy_context function to use in environments without context vars
  • Adds reset functionality to our threadlocal based context vars.

This is preparation work for refactoring how we deal with Hubs and Scopes in the future.

Comment on lines 1300 to +1319
try:
from aiocontextvars import ContextVar
from aiocontextvars import ContextVar, copy_context

return True, ContextVar
return True, ContextVar, copy_context
except ImportError:
pass
else:
# On Python 3.7 contextvars are functional.
try:
from contextvars import ContextVar
from contextvars import ContextVar, copy_context

return True, ContextVar
return True, ContextVar, copy_context
except ImportError:
pass

# Fall back to basic thread-local usage.

from threading import local

return False, _make_threadlocal_contextvars(local)
return False, _make_threadlocal_contextvars(local), _make_noop_copy_context()
Copy link

@50-Course 50-Course Dec 11, 2023

Choose a reason for hiding this comment

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

Looks good to me

@50-Course
Copy link

Following this MR 👍🏼 looking forward to the updates

@antonpirker antonpirker changed the base branch from master to feat/new-scopes December 11, 2023 16:07
@antonpirker antonpirker changed the base branch from feat/new-scopes to antonpirker/refactor-hub-transaction-span-continue-trace December 11, 2023 16:09
@antonpirker antonpirker changed the title Add reset() to thread local ContextVar and no-op copy_context() (5) Add reset() to thread local ContextVar and no-op copy_context() Dec 11, 2023
@antonpirker antonpirker changed the base branch from antonpirker/refactor-hub-transaction-span-continue-trace to feat/new-scopes December 11, 2023 16:15
@antonpirker antonpirker marked this pull request as ready for review December 14, 2023 14:10
Copy link
Member

@sl0thentr0py sl0thentr0py left a comment

Choose a reason for hiding this comment

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

I don't yet know how you will use copy_context but this change is fine self-contained

def reset(self, token):
# type: (Any) -> None
self._local.value = getattr(self._original_local, token)
setattr(self._original_local, token, None)
Copy link
Member

Choose a reason for hiding this comment

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

isn't it better to delete here so that it gets GCd?

Copy link

@50-Course 50-Course Dec 19, 2023

Choose a reason for hiding this comment

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

Hi @sl0thentr0py, please can you go into the detail about this tradeoff?

Thank you.

Copy link
Member Author

Choose a reason for hiding this comment

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

@50-Course If it is not garbage collected it stays in memory. With a lot sets and resets the memory usage could grow without memory being freed.

Choose a reason for hiding this comment

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

Oh okay got it!

@antonpirker antonpirker merged commit eff8f78 into feat/new-scopes Dec 19, 2023
120 checks passed
@antonpirker antonpirker deleted the antonpirker/update-threadlocal-contextvars branch December 19, 2023 08:30
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.

None yet

3 participants