Skip to content

wyfo/contextclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

contextclasses - Context classes

Context classes provide a typed interface over a context variable dictionary. It can be used for example to reproduce Flask (thread-local) "global" context in an async web framework.

Example

import asyncio
from contextclasses import contextclass


async def test_use_case():
    @contextclass
    class Ctx:
        n: int

    async def use_ctx(n: int):
        Ctx.n = n
        await asyncio.sleep(0) # switch context
        return Ctx.n

    values = [*range(5)]
    result = await asyncio.gather(*(use_ctx(n) for n in values))
    assert result == values

For more examples, please look at the tests

About

Easy typed interface for ContextVar

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages