Global thread context manager
Project description
global-manager
Global thread context manager.
Installation
pip install global-manager
Examples
from typing import Optional
import dataclasses
import logging
from global_manager import GlobalManager
logger = logging.getLogger('my_view')
@dataclasses.dataclass
class UserContext:
"""Some context"""
ip: str
profile_id: int
class UserContextManager(GlobalManager[UserContext]):
@classmethod
def get_current_user_context(cls) -> Optional[UserContext]:
"""a business meaning method name is a good practice"""
return cls.get_current_context()
def my_func():
user_context: Optional[UserContext] = UserContextManager.get_current_user_context()
# retrieve context
logger.debug('my_view profile_id=%s, ip=%s', user_context.profile_id, user_context.ip)
# other logic ...
# some where in code, m.b. in web framework
def my_view(request):
# Imagine that the data is obtained from the request
user_context = UserContext(ip='127.0.0.1', profile_id=1)
with UserContext(user_context):
return my_func()
Context in context.
with UserContext(uc1):
UserContext.get_current_user_context() # uc1 context
with UserContext(uc2):
UserContext.get_current_user_context() # uc2 context
UserContext.get_current_user_context() # uc1 context
UserContext.get_current_user_context() # None
Async notations is supported too.
async with UserContext(uc1):
UserContext.get_current_user_context() # uc1 context
async with UserContext(uc2):
UserContext.get_current_user_context() # uc2 context
UserContext.get_current_user_context() # uc1 context
UserContext.get_current_user_context() # None
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
global_manager-1.0.4.tar.gz
(3.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file global_manager-1.0.4.tar.gz.
File metadata
- Download URL: global_manager-1.0.4.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5684025aa3281b27948e3df681331181bbe7c294b743839034087d3e94629e3e
|
|
| MD5 |
88fe445c94eb561a1246097aedcebf30
|
|
| BLAKE2b-256 |
1f8644780fb8b1e167ee00000db6a1a542d38866f5e43c8b06c9ebc74245f963
|
File details
Details for the file global_manager-1.0.4-py3-none-any.whl.
File metadata
- Download URL: global_manager-1.0.4-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2fee46be370768cf5367b0df67a540a1586184523f55049a40e6f970545681c
|
|
| MD5 |
c3237b9e9f147d86a504a798efb534c9
|
|
| BLAKE2b-256 |
1593cd616b1e13ca76942b30d03c125a6416cd386e9389db5fd0d43992b2e299
|