File tree Expand file tree Collapse file tree 2 files changed +11
-14
lines changed
Expand file tree Collapse file tree 2 files changed +11
-14
lines changed Original file line number Diff line number Diff line change @@ -133,8 +133,8 @@ Default bus
133133++++++++++++++++++++++++++
134134
135135Most object methods that take a bus as a parameter
136- will use a default bus connection if a bus object is
137- not explicitly passed.
136+ will use a thread-local default bus connection if a bus object
137+ is not explicitly passed.
138138
139139Session bus is default bus when running as a user and
140140system bus otherwise.
@@ -167,4 +167,4 @@ Contents
167167++++++++++++++++++++
168168* :ref: `genindex `
169169* :doc: `/api_index `
170- * :ref: `search `
170+ * :ref: `search `
Original file line number Diff line number Diff line change 2121from __future__ import annotations
2222
2323from asyncio import get_running_loop
24- from typing import Iterator , Optional
24+ from contextvars import ContextVar
25+ from typing import Iterator
2526
2627from .sd_bus_internals import (
2728 DbusPropertyConstFlag ,
3233 sd_bus_open ,
3334)
3435
35- DEFAULT_BUS : Optional [SdBus ] = None
36+ DEFAULT_BUS : ContextVar [SdBus ] = ContextVar ( 'DEFAULT_BUS' )
3637
3738PROPERTY_FLAGS_MASK = (
3839 DbusPropertyConstFlag | DbusPropertyEmitsChangeFlag |
@@ -50,20 +51,16 @@ def _is_property_flags_correct(flags: int) -> bool:
5051
5152
5253def get_default_bus () -> SdBus :
53- global DEFAULT_BUS
54- old_bus = DEFAULT_BUS
55- if old_bus is None :
54+ try :
55+ return DEFAULT_BUS . get ()
56+ except LookupError :
5657 new_bus = sd_bus_open ()
57- DEFAULT_BUS = new_bus
58+ DEFAULT_BUS . set ( new_bus )
5859 return new_bus
59- else :
60- return old_bus
6160
6261
6362def set_default_bus (new_default : SdBus ) -> None :
64- global DEFAULT_BUS
65-
66- DEFAULT_BUS = new_default
63+ DEFAULT_BUS .set (new_default )
6764
6865
6966async def request_default_bus_name_async (
You can’t perform that action at this time.
0 commit comments