本文整理汇总了Python中threading.local方法的典型用法代码示例。如果您正苦于以下问题:Python threading.local方法的具体用法?Python threading.local怎么用?Python threading.local使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading
的用法示例。
在下文中一共展示了threading.local方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def __init__(self, *args, **kwargs):
class LocalContexts(threading.local):
def __init__(self):
super(LocalContexts, self).__init__()
self._contexts = []
def append(self, item):
self._contexts.append(item)
def pop(self):
return self._contexts.pop()
super(ThreadSafeStackContext, self).__init__(*args, **kwargs)
if hasattr(self, 'contexts'):
# only patch if context exists
self.contexts = LocalContexts()
示例2: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def __init__(self, threads=4, *args, **kwargs):
self.local = threading.local()
super(ThreadBaseScheduler, self).__init__(*args, **kwargs)
if isinstance(self.taskdb, SQLiteMixin):
self.threads = 1
else:
self.threads = threads
self._taskdb = self.taskdb
self._projectdb = self.projectdb
self._resultdb = self.resultdb
self.thread_objs = []
self.thread_queues = []
self._start_threads()
assert len(self.thread_queues) > 0
示例3: __exit__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def __exit__(self, type, value, traceback):
try:
self.exit(type, value, traceback)
finally:
final_contexts = _state.contexts
_state.contexts = self.old_contexts
# Generator coroutines and with-statements with non-local
# effects interact badly. Check here for signs of
# the stack getting out of sync.
# Note that this check comes after restoring _state.context
# so that if it fails things are left in a (relatively)
# consistent state.
if final_contexts is not self.new_contexts:
raise StackContextInconsistentError(
'stack_context inconsistency (may be caused by yield '
'within a "with StackContext" block)')
# Break up a reference to itself to allow for faster GC on CPython.
self.new_contexts = None
示例4: emit
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def emit(self, record: logging.LogRecord):
"""Print the log record formatted as JSON to stdout."""
created = datetime.datetime.fromtimestamp(record.created, timezone)
obj = {
"level": record.levelname.lower(),
"msg": record.msg % record.args,
"source": "%s:%d" % (record.filename, record.lineno),
"time": format_datetime(created),
"thread": reduce_thread_id(record.thread),
}
if record.exc_info is not None:
obj["error"] = traceback.format_exception(*record.exc_info)[1:]
try:
obj["context"] = self.local.context
except AttributeError:
pass
json.dump(obj, sys.stdout, sort_keys=True)
sys.stdout.write("\n")
sys.stdout.flush()
示例5: _put
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def _put(self, lpath, rpath, recursive=False, **kwargs):
"""copy local files to remote
"""
from .implementations.local import make_path_posix, LocalFileSystem
rpath = self._strip_protocol(rpath)
if isinstance(lpath, str):
lpath = make_path_posix(lpath)
fs = LocalFileSystem()
lpaths = fs.expand_path(lpath, recursive=recursive)
rpaths = other_paths(lpaths, rpath)
await asyncio.gather(
*[
self._put_file(lpath, rpath, **kwargs)
for lpath, rpath in zip(lpaths, rpaths)
]
)
示例6: _init
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def _init(self):
endpoint, kwargs = self._endpoint, self._kws
if endpoint is not None:
if 'http' in endpoint:
# connect to web
from .web.session import Session as WebSession
self._sess = WebSession(endpoint, **kwargs)
else:
# connect to local cluster
self._sess = ClusterSession(endpoint, **kwargs)
else:
try:
endpoint = os.environ['MARS_SCHEDULER_ADDRESS']
session_id = os.environ.get('MARS_SESSION_ID', None)
self._sess = ClusterSession(endpoint, session_id=session_id)
except KeyError:
self._sess = LocalSession(**kwargs)
示例7: open_rpc_interface
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def open_rpc_interface(self):
try:
self.local.rpc_interface.close()
except Exception: # pylint: disable=W0703
pass
for x in range(100):
try:
self.local.rpc_interface = common.get_rpyc.RemoteJobInterface("RawMirror")
return
except (TypeError, KeyError, socket.timeout, ConnectionRefusedError):
for line in traceback.format_exc().split("\n"):
self.log.error(line)
if x > 90:
raise RuntimeError("Could not establish connection to RPC remote!")
raise RuntimeError("Could not establish connection to RPC remote!")
示例8: template_localtime
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def template_localtime(value, use_tz=None):
"""
Checks if value is a datetime and converts it to local time if necessary.
If use_tz is provided and is not None, that will force the value to
be converted (or not), overriding the value of settings.USE_TZ.
This function is designed for use by the template engine.
"""
should_convert = (isinstance(value, datetime)
and (settings.USE_TZ if use_tz is None else use_tz)
and not is_naive(value)
and getattr(value, 'convert_to_local_time', True))
return localtime(value) if should_convert else value
# Utilities
示例9: localtime
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def localtime(value, timezone=None):
"""
Converts an aware datetime.datetime to local time.
Local time is defined by the current time zone, unless another time zone
is specified.
"""
if timezone is None:
timezone = get_current_timezone()
# If `value` is naive, astimezone() will raise a ValueError,
# so we don't need to perform a redundant check.
value = value.astimezone(timezone)
if hasattr(timezone, 'normalize'):
# This method is available for pytz time zones.
value = timezone.normalize(value)
return value
示例10: set_trace
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def set_trace(self, frame=None):
"""Remember starting frame.
This is used with pytest, which does not use pdb.set_trace().
"""
if getattr(local, "_pdbpp_completing", False):
# Handle set_trace being called during completion, e.g. with
# fancycompleter's attr_matches.
return
if self.disabled:
return
if frame is None:
frame = sys._getframe().f_back
self._via_set_trace_frame = frame
self._stopped_for_set_trace = False
self.start_filename = frame.f_code.co_filename
self.start_lineno = frame.f_lineno
return super(Pdb, self).set_trace(frame)
示例11: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def __init__(
self,
proxy=None,
session=None,
timeout=80,
verify_ssl=True,
**kwargs
):
# type: (str, str, str or dict, _requests.Session, int, bool) -> HTTPClient
self._proxy = None
self._session = session
self._timeout = timeout
self._verify_ssl = verify_ssl
self._kwargs = _copy.deepcopy(kwargs)
if proxy:
if isinstance(proxy, str):
proxy = { "http": proxy, "https": proxy }
if not isinstance(proxy, dict):
raise ValueError(
"""
The `proxy` parameter must either be `None`, a string
representing the URL of a proxy for both HTTP + HTTPS,
or a dictionary with a different URL for `"http"` and
`"https"`.
"""
)
self._proxy = _copy.deepcopy(proxy)
# NOTE: This make HTTPClient and any class containing it as an attribute
# impossible to pickle. Implemented custom pickling to avoid this.
self._local_thread = _threading.local()
示例12: _get_session
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def _get_session(self):
# type: None -> _threading.local
"""
Return or establish the session associated with the current thread.
"""
# Make sure the local thread storage has been instantiated
if getattr(self, "_local_thread", None) is None:
self._local_thread = _threading.local()
# Store whatever session we use in the local thread storage
if getattr(self._local_thread, "session", None) is None:
self._local_thread.session = self._session or _requests.Session()
return self._local_thread.session
示例13: __setstate__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def __setstate__(self, state):
self.__dict__ = state
if self.__dict__.get("_local_thread", None) is None:
self.__dict__["_local_thread"] = _threading.local()
return self
示例14: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def __init__(self):
self._tls_scope = threading.local()
示例15: activate
# 需要导入模块: import threading [as 别名]
# 或者: from threading import local [as 别名]
def activate(self, span, finish_on_close):
"""
Make a :class:`~opentracing.Span` instance active.
:param span: the :class:`~opentracing.Span` that should become active.
:param finish_on_close: whether *span* should automatically be
finished when :meth:`Scope.close()` is called.
If no :func:`tracer_stack_context()` is detected, thread-local
storage will be used to store the :class:`~opentracing.Scope`.
Observe that in this case the active :class:`~opentracing.Span`
will not be automatically propagated to the child corotuines.
:return: a :class:`~opentracing.Scope` instance to control the end
of the active period for the :class:`~opentracing.Span`.
It is a programming error to neglect to call :meth:`Scope.close()`
on the returned instance.
"""
context = self._get_context()
if context is None:
return super(TornadoScopeManager, self).activate(span,
finish_on_close)
scope = _TornadoScope(self, span, finish_on_close)
context.active = scope
return scope