Skip to main content

Global thread context manager

Project description

global-manager

Tests codecov Mypy pypi downloads versions

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.3.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

global_manager-1.0.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file global_manager-1.0.3.tar.gz.

File metadata

  • Download URL: global_manager-1.0.3.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

Hashes for global_manager-1.0.3.tar.gz
Algorithm Hash digest
SHA256 20a5763943010dca976b7ecc39b5298281954735d282b9dd3497d5b03408d750
MD5 939bfa81d1027a1ec0b5814f50860652
BLAKE2b-256 9d9618b68bf149c3b3542e9c59574e92c8cd97c75c5d93e5ec2f845aeda606f7

See more details on using hashes here.

File details

Details for the file global_manager-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: global_manager-1.0.3-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

Hashes for global_manager-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ec66446c5cd8fb1550efe106b5921bbcaafc55993a798d226441942bfba365fb
MD5 3d0943ed44190fdb40c6725793413346
BLAKE2b-256 c773bbcb91ed9cfa4297b678da514a2e268f3bbb18d849cbea9f3447ac7455bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page