跳转到主要内容

OpenCensus 运行时上下文

项目描述

pypi

OpenCensus 运行时上下文提供了进程内上下文传播。默认情况下,对于 Python 2.7、3.4 和 3.5,使用 线程本地存储;对于 Python >= 3.6,使用 contextvars,这提供了对 asyncio 的支持。

安装

此库默认与 opencensus 一起安装,无需显式安装。

使用

在大多数情况下,上下文传播在进程内自动发生,遵循线程和异步协程的控制流。当可用时,运行时上下文是存储在 上下文变量 中的字典,否则存储在 线程本地存储 中。

有些情况下,您可能需要显式传播上下文

显式线程创建

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()

线程池

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()

参考

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定要选择哪一个,请了解更多关于 安装包 的信息。

源分发

opencensus-context-0.1.3.tar.gz (4.1 kB 查看散列)

上传时间

构建分发

opencensus_context-0.1.3-py2.py3-none-any.whl (5.1 kB 查看散列)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面