Skip to main content

Context manager that provides a means for context to be set, and retrieved in Python AsyncIO.

Project description

AIO Task Bound Context

Context manager that provides a means for context to be set, and retrieved in Python AsyncIO.

What???

Okay so for a concrete example, thing of how Flask handles the current request:

from flask import request

This import, called from anywhere, will import the current request being handled. This is made possible in a way similar to this:

request = None
def get_request():
    return request
def set_request(value):
    global request
    request = value

When the HTTP server gets a request, it will call set_request, then anywhere in the code another function can call get_request to get the value.

Here's the kicker: This is not possible with AIO, because multiple tasks may be running at once, so there are multiple values for request, rather than just a single value. Imagine the same piece of code being used in AIO:

import asyncio as aio

async def handle_request(request):
    set_request(request)
    # generate the response
    await aio.sleep(1)
    assert get_request() == request  # will fail
    set_request(None)

aio.get_event_loop().run_until_complete(aio.gather(
    handle_request('value 1'),
    handle_request('value 2'),
))

Obviously, this is going to be problematic.

The answer

aio_task_bound_context attaches a stack of the current context values to the current Task, as well as tracking the parent tasks so that their context can be inherrited:

import asyncio as aio
from aio_task_bound_context import set_task_factory, TaskBoundContext

class RequestContext(TaskBoundContext):
    def __init__(self, request):
        self.request = request
    def get_value(self):
        return self.request

async def handle_request(request):
    with RequestContext(request):
        # generate the response
        await aio.sleep(1)
        assert RequestContext.current() == request # will succeed

loop = aio.get_event_loop()
set_task_factory(loop=loop)
loop.run_until_complete(aio.gather(
    handle_request('value 1'),
    handle_request('value 2'),
))

Examples

Note that all these examples will work in async tasks, which is what makes them more special than a simple context manager. They are all simple examples outside an async environment, but don't be fooled by the hidden complexity.

To start off, we need to replace the default task factory in asyncio with a wrapper to add extra details to tasks. Assume this has been executed before all examples:

import asyncio as aio
from aio_task_bound_context import create_task_factory, TaskBoundContext
loop = aio.get_event_loop()
loop.set_task_factory(create_task_factory(loop=loop))

With no get_value function defined, the "value" is the TaskBoundContext itself, so you can setup values in the __init__ function if you just want to pass around as set of values.

class ExampleContext(TaskBoundContext):
    def __init__(self, *args, **kwargs):
        super().__init__()
        self.args = args
        self.kwargs = kwargs
with ExampleContext('an arg', key='in kwargs'):
    assert ExampleContext.current().args == ['an arg']
    assert ExampleContext.current().kwargs == {'key': 'in kwargs'}

The "as value" of the context manager is the value returned from get_value.

class ExampleContext(TaskBoundContext):
    def __init__(self, value):
        super().__init__()
        self.value = value
    def get_value(self):
        return self.value
with ExampleContext('test') as value:
    assert value == 'test'
with ExampleContext('different') as value:
    assert value == 'different'

Contexts are a hierarchical stack, so you can have multiple contexts and they will push/pop their values onto/off of the stack of contexts.

class ExampleContext(TaskBoundContext):
    def __init__(self, value):
        super().__init__()
        self.value = value
    def get_value(self):
        return self.value
with ExampleContext('first'):
    assert ExampleContext.current() == 'first'
    with ExampleContext('second'):
        assert ExampleContext.current() == 'second'
    assert ExampleContext.current() == 'first

The get_value function can accept a single argument, which is the current value of the stack.

class LoggerContext(TaskBoundContext):
    def __init__(self, suffix):
        super().__init__()
        self.suffix = suffix
    def get_value(self, current):
        if current is None:
            return logging.getLogger(self.suffix)
        else:
            return current.getChild(self.suffix)

Testing

Python 3.5+ is supported. To run tests across all environments, we use pyenv, and some quick virtualenv invocations (yes, we could also use tox).

To run the tests, just run ./tests_runall.sh which will install relevant Python versions if not already installed, create virtualenvs for them, and run tests.py.

To run tests manually, simply ./test.py.

License

Copyright 2018 Ricky Cook

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

aio_task_bound_context-0.2.4.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file aio_task_bound_context-0.2.4.tar.gz.

File metadata

  • Download URL: aio_task_bound_context-0.2.4.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.3

File hashes

Hashes for aio_task_bound_context-0.2.4.tar.gz
Algorithm Hash digest
SHA256 2c0eb1363527116b3489ce85bda3ab9e70707203a7e59e5821327ec44e0084b7
MD5 623fd7618b5e2f4b34d5fd2a4530ced5
BLAKE2b-256 da7509cc6f4c9a83676ec24124c8ee36224a6e22dcb46e62af3d4ea9a3c7ee1a

See more details on using hashes here.

Supported by

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