Skip to main content

OpenCensus Runtime Context

Project description

pypi

The OpenCensus Runtime Context provides in-process context propagation. By default, thread local storage is used for Python 2.7, 3.4 and 3.5; contextvars is used for Python >= 3.6, which provides asyncio support.

Installation

This library is installed by default with opencensus, there is no need to install it explicitly.

Usage

In most cases context propagation happens automatically within a process, following the control flow of threads and asynchronous coroutines. The runtime context is a dictionary stored in a context variable when available, and in thread local storage otherwise.

There are cases where you may want to propagate the context explicitly:

Explicit Thread Creation

from threading import Thread
from opencensus.common.runtime_context import RuntimeContext

def work(name):
    # here you will get the context from the parent thread
    print(RuntimeContext)

thread = Thread(
    # propagate context explicitly
    target=RuntimeContext.with_current_context(work),
    args=('foobar',),
)
thread.start()
thread.join()

Thread Pool

from multiprocessing.dummy import Pool as ThreadPool
from opencensus.common.runtime_context import RuntimeContext

def work(name):
    # here you will get the context from the parent thread
    print(RuntimeContext)

pool = ThreadPool(2)
# propagate context explicitly
pool.map(RuntimeContext.with_current_context(work), [
    'bear',
    'cat',
    'dog',
    'horse',
    'rabbit',
])
pool.close()
pool.join()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for opencensus-context, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size opencensus_context-0.1.2-py2.py3-none-any.whl (4.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size opencensus-context-0.1.2.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page