Python threading.local() Examples

The following are 30 code examples for showing how to use threading.local(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module threading , or try the search function .

Example 1
Project: opentracing-python   Author: opentracing   File: tornado.py    License: Apache License 2.0 6 votes vote down vote up
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() 
Example 2
Project: pyspider   Author: binux   File: scheduler.py    License: Apache License 2.0 6 votes vote down vote up
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 
Example 3
Project: tornado-zh   Author: tao12345666333   File: stack_context.py    License: MIT License 6 votes vote down vote up
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 
Example 4
Project: tornado-zh   Author: tao12345666333   File: stack_context.py    License: MIT License 6 votes vote down vote up
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 
Example 5
Project: modelforge   Author: src-d   File: slogging.py    License: Apache License 2.0 6 votes vote down vote up
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() 
Example 6
Project: filesystem_spec   Author: intake   File: asyn.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)
            ]
        ) 
Example 7
Project: mars   Author: mars-project   File: session.py    License: Apache License 2.0 6 votes vote down vote up
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) 
Example 8
Project: ReadableWebProxy   Author: fake-name   File: RawJobDispatcher.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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!") 
Example 9
Project: GTDWeb   Author: lanbing510   File: timezone.py    License: GNU General Public License v2.0 6 votes vote down vote up
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 
Example 10
Project: GTDWeb   Author: lanbing510   File: timezone.py    License: GNU General Public License v2.0 6 votes vote down vote up
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 
Example 11
Project: pdbpp   Author: pdbpp   File: pdbpp.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example 12
Project: codepost-python   Author: codepost-io   File: http_client.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
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() 
Example 13
Project: codepost-python   Author: codepost-io   File: http_client.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example 14
Project: codepost-python   Author: codepost-io   File: http_client.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def __setstate__(self, state):
        self.__dict__ = state
        if self.__dict__.get("_local_thread", None) is None:
            self.__dict__["_local_thread"] = _threading.local()
        return self 
Example 15
Project: opentracing-python   Author: opentracing   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self._tls_scope = threading.local() 
Example 16
Project: opentracing-python   Author: opentracing   File: tornado.py    License: Apache License 2.0 5 votes vote down vote up
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 
Example 17
Project: opentracing-python   Author: opentracing   File: tornado.py    License: Apache License 2.0 5 votes vote down vote up
def tracer_stack_context():
    """
    Create a custom Tornado's :class:`StackContext` that allows
    :class:`TornadoScopeManager` to store the active
    :class:`~opentracing.Span` in the thread-local request context.

    Suppose you have a method ``handle_request(request)`` in the
    http server. Instead of calling it directly, use a wrapper:

    .. code-block:: python

        from opentracing.scope_managers.tornado import tracer_stack_context

        @tornado.gen.coroutine
        def handle_request_wrapper(request, actual_handler, *args, **kwargs)

            request_wrapper = TornadoRequestWrapper(request=request)
            span = http_server.before_request(request=request_wrapper)

            with tracer_stack_context():
                with tracer.scope_manager.activate(span, True):
                    return actual_handler(*args, **kwargs)

    :return:
        Return a custom :class:`StackContext` that allows
        :class:`TornadoScopeManager` to activate and propagate
        :class:`~opentracing.Span` instances.
    """
    context = _TracerRequestContext()
    return ThreadSafeStackContext(
            lambda: _TracerRequestContextManager(context)
    ) 
Example 18
Project: gnocchi   Author: gnocchixyz   File: sqlalchemy.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, conf):
        self.trans = enginefacade.transaction_context()
        self.trans.configure(
            **dict(conf.database.items())
        )
        self._context = threading.local() 
Example 19
Project: OpenTrader   Author: OpenTrading   File: ListenerThread.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, sChartId, **dArgs):

        self.oLocal = threading.local()
        self._running = threading.Event()

        self.lCharts = []
        self.jLastRetval = []
        self.jLastTick = []
        self.jLastBar = []
        self.gLastTimer = []
        self.dRetvals = {}
        self.bPprint = False
        self.lHide = [] 
Example 20
Project: gist-alfred   Author: danielecook   File: auth.py    License: MIT License 5 votes vote down vote up
def __init__(self, username, password):
        self.username = username
        self.password = password
        # Keep state in per-thread local storage
        self._thread_local = threading.local() 
Example 21
Project: pyspider   Author: binux   File: scheduler.py    License: Apache License 2.0 5 votes vote down vote up
def taskdb(self):
        if not hasattr(self.local, 'taskdb'):
            self.taskdb = self._taskdb.copy()
        return self.local.taskdb 
Example 22
Project: pyspider   Author: binux   File: scheduler.py    License: Apache License 2.0 5 votes vote down vote up
def taskdb(self, taskdb):
        self.local.taskdb = taskdb 
Example 23
Project: pyspider   Author: binux   File: scheduler.py    License: Apache License 2.0 5 votes vote down vote up
def projectdb(self):
        if not hasattr(self.local, 'projectdb'):
            self.projectdb = self._projectdb.copy()
        return self.local.projectdb 
Example 24
Project: pyspider   Author: binux   File: scheduler.py    License: Apache License 2.0 5 votes vote down vote up
def projectdb(self, projectdb):
        self.local.projectdb = projectdb 
Example 25
Project: pyspider   Author: binux   File: scheduler.py    License: Apache License 2.0 5 votes vote down vote up
def resultdb(self, resultdb):
        self.local.resultdb = resultdb 
Example 26
Project: crowdata   Author: crowdata   File: middleware.py    License: MIT License 5 votes vote down vote up
def _set_current_user(user=None):
    '''
    Sets current user in local thread.

    Can be used as a hook e.g. for shell jobs (when request object is not
    available).
    '''
    _do_set_current_user(lambda self: user) 
Example 27
Project: misp42splunk   Author: remg427   File: auth.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, username, password):
        self.username = username
        self.password = password
        # Keep state in per-thread local storage
        self._thread_local = threading.local() 
Example 28
Project: misp42splunk   Author: remg427   File: auth.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, username, password):
        self.username = username
        self.password = password
        # Keep state in per-thread local storage
        self._thread_local = threading.local() 
Example 29
Project: misp42splunk   Author: remg427   File: auth.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, username, password):
        self.username = username
        self.password = password
        # Keep state in per-thread local storage
        self._thread_local = threading.local() 
Example 30
Project: misp42splunk   Author: remg427   File: auth.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, username, password):
        self.username = username
        self.password = password
        # Keep state in per-thread local storage
        self._thread_local = threading.local()