Global thread context manager
Project description
global-manager
Global thread context manager.
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):
@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
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.1.tar.gz
(3.5 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.1.tar.gz.
File metadata
- Download URL: global_manager-1.0.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb0735454ed3ce4af87135c39e8d2e438772e5385c969c5055f5fb2e8bb04308
|
|
| MD5 |
44b7d91e41ebc24e4deb2ef47c5cf2de
|
|
| BLAKE2b-256 |
1fd2ce7d49393c375c17daf7bcbf309dcb8dd5896de8755d47338944531dbae3
|
File details
Details for the file global_manager-1.0.1-py3-none-any.whl.
File metadata
- Download URL: global_manager-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bf1a9888576d5c151c5a64ce4e343033e5c2554f8793f72c130754f24b042a9
|
|
| MD5 |
62a85fe55966e18b7317f5d7386aced6
|
|
| BLAKE2b-256 |
daffac720a9902e4590833ca92aca6fd15ea647b3b76aeea602ade66b890b10b
|