PHP 8.1.33
Preview: _threading_local.py Size: 7.04 KB
/usr/lib64/python3.6/_threading_local.py

"""Thread-local objects.

(Note that this module provides a Python version of the threading.local
 class.  Depending on the version of Python you're using, there may be a
 faster one available.  You should always import the `local` class from
 `threading`.)

Thread-local objects support the management of thread-local data.
If you have data that you want to be local to a thread, simply create
a thread-local object and use its attributes:

  >>> mydata = local()
  >>> mydata.number = 42
  >>> mydata.number
  42

You can also access the local-object's dictionary:

  >>> mydata.__dict__
  {'number': 42}
  >>> mydata.__dict__.setdefault('widgets', [])
  []
  >>> mydata.widgets
  []

What's important about thread-local objects is that their data are
local to a thread. If we access the data in a different thread:

  >>> log = []
  >>> def f():
  ...     items = sorted(mydata.__dict__.items())
  ...     log.append(items)
  ...     mydata.number = 11
  ...     log.append(mydata.number)

  >>> import threading
  >>> thread = threading.Thread(target=f)
  >>> thread.start()
  >>> thread.join()
  >>> log
  [[], 11]

we get different data.  Furthermore, changes made in the other thread
don't affect data seen in this thread:

  >>> mydata.number
  42

Of course, values you get from a local object, including a __dict__
attribute, are for whatever thread was current at the time the
attribute was read.  For that reason, you generally don't want to save
these values across threads, as they apply only to the thread they
came from.

You can create custom local objects by subclassing the local class:

  >>> class MyLocal(local):
  ...     number = 2
  ...     def __init__(self, **kw):
  ...         self.__dict__.update(kw)
  ...     def squared(self):
  ...         return self.number ** 2

This can be useful to support default values, methods and
initialization.  Note that if you define an __init__ method, it will be
called each time the local object is used in a separate thread.  This
is necessary to initialize each thread's dictionary.

Now if we create a local object:

  >>> mydata = MyLocal(color='red')

Now we have a default number:

  >>> mydata.number
  2

an initial color:

  >>> mydata.color
  'red'
  >>> del mydata.color

And a method that operates on the data:

  >>> mydata.squared()
  4

As before, we can access the data in a separate thread:

  >>> log = []
  >>> thread = threading.Thread(target=f)
  >>> thread.start()
  >>> thread.join()
  >>> log
  [[('color', 'red')], 11]

without affecting this thread's data:

  >>> mydata.number
  2
  >>> mydata.color
  Traceback (most recent call last):
  ...
  AttributeError: 'MyLocal' object has no attribute 'color'

Note that subclasses can define slots, but they are not thread
local. They are shared across threads:

  >>> class MyLocal(local):
  ...     __slots__ = 'number'

  >>> mydata = MyLocal()
  >>> mydata.number = 42
  >>> mydata.color = 'red'

So, the separate thread:

  >>> thread = threading.Thread(target=f)
  >>> thread.start()
  >>> thread.join()

affects what we see:

  >>> mydata.number
  11

>>> del mydata
"""

from weakref import ref
from contextlib import contextmanager

__all__ = ["local"]

# We need to use objects from the threading module, but the threading
# module may also want to use our `local` class, if support for locals
# isn't compiled in to the `thread` module.  This creates potential problems
# with circular imports.  For that reason, we don't import `threading`
# until the bottom of this file (a hack sufficient to worm around the
# potential problems).  Note that all platforms on CPython do have support
# for locals in the `thread` module, and there is no circular import problem
# then, so problems introduced by fiddling the order of imports here won't
# manifest.

class _localimpl:
    """A class managing thread-local dicts"""
    __slots__ = 'key', 'dicts', 'localargs', 'locallock', '__weakref__'

    def __init__(self):
        # The key used in the Thread objects' attribute dicts.
        # We keep it a string for speed but make it unlikely to clash with
        # a "real" attribute.
        self.key = '_threading_local._localimpl.' + str(id(self))
        # { id(Thread) -> (ref(Thread), thread-local dict) }
        self.dicts = {}

    def get_dict(self):
        """Return the dict for the current thread. Raises KeyError if none
        defined."""
        thread = current_thread()
        return self.dicts[id(thread)][1]

    def create_dict(self):
        """Create a new dict for the current thread, and return it."""
        localdict = {}
        key = self.key
        thread = current_thread()
        idt = id(thread)
        def local_deleted(_, key=key):
            # When the localimpl is deleted, remove the thread attribute.
            thread = wrthread()
            if thread is not None:
                del thread.__dict__[key]
        def thread_deleted(_, idt=idt):
            # When the thread is deleted, remove the local dict.
            # Note that this is suboptimal if the thread object gets
            # caught in a reference loop. We would like to be called
            # as soon as the OS-level thread ends instead.
            local = wrlocal()
            if local is not None:
                dct = local.dicts.pop(idt)
        wrlocal = ref(self, local_deleted)
        wrthread = ref(thread, thread_deleted)
        thread.__dict__[key] = wrlocal
        self.dicts[idt] = wrthread, localdict
        return localdict


@contextmanager
def _patch(self):
    impl = object.__getattribute__(self, '_local__impl')
    try:
        dct = impl.get_dict()
    except KeyError:
        dct = impl.create_dict()
        args, kw = impl.localargs
        self.__init__(*args, **kw)
    with impl.locallock:
        object.__setattr__(self, '__dict__', dct)
        yield


class local:
    __slots__ = '_local__impl', '__dict__'

    def __new__(cls, *args, **kw):
        if (args or kw) and (cls.__init__ is object.__init__):
            raise TypeError("Initialization arguments are not supported")
        self = object.__new__(cls)
        impl = _localimpl()
        impl.localargs = (args, kw)
        impl.locallock = RLock()
        object.__setattr__(self, '_local__impl', impl)
        # We need to create the thread dict in anticipation of
        # __init__ being called, to make sure we don't call it
        # again ourselves.
        impl.create_dict()
        return self

    def __getattribute__(self, name):
        with _patch(self):
            return object.__getattribute__(self, name)

    def __setattr__(self, name, value):
        if name == '__dict__':
            raise AttributeError(
                "%r object attribute '__dict__' is read-only"
                % self.__class__.__name__)
        with _patch(self):
            return object.__setattr__(self, name, value)

    def __delattr__(self, name):
        if name == '__dict__':
            raise AttributeError(
                "%r object attribute '__dict__' is read-only"
                % self.__class__.__name__)
        with _patch(self):
            return object.__delattr__(self, name)


from threading import current_thread, RLock

Directory Contents

Dirs: 30 × Files: 170

Name Size Perms Modified Actions
asyncio DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-08-29 08:32:23
Edit Download
ctypes DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
curses DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
dbm DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
distutils DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
email DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
encodings DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
ensurepip DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
html DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
http DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
importlib DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
json DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
lib2to3 DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
logging DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-10-31 08:30:39
Edit Download
sqlite3 DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
test DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
unittest DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
urllib DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
venv DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
wsgiref DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
xml DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
xmlrpc DIR
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
- drwxr-xr-x 2025-08-29 08:32:10
Edit Download
8.52 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
31.69 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
477 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
88.25 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
11.88 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
11.06 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
19.69 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
19.91 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
23.00 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
13.63 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.53 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
12.19 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
22.67 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
36.35 KB lrwxr-xr-x 2025-08-26 08:58:55
Edit Download
11.74 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.30 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
14.51 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
10.37 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
35.43 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.85 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.97 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
11.84 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
52.34 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
12.85 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.61 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
6.84 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.25 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
1.82 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
15.80 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
80.11 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
320 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
82.40 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
17.71 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
101.94 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.75 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
32.82 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
9.60 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
14.13 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.09 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
14.79 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
23.08 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
34.78 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
30.61 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
4.91 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
7.31 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.85 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
21.03 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.51 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
19.86 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.59 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
22.39 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
6.23 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
52.05 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.71 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
10.42 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
114.22 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.43 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
75.99 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
2.17 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
5.19 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
75.49 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
12.68 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.83 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.67 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
76.78 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.85 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
20.55 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
22.49 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.55 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
42.07 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
22.55 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.39 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
10.00 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.69 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
10.61 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
58.96 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
36.65 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
45.15 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
59.88 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
54.39 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
89.62 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.71 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
20.82 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
46.11 KB lrwxr-xr-x 2025-08-26 08:58:55
Edit Download
31.53 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
14.61 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
15.94 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
20.37 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
21.51 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
25.94 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
4.65 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
13.24 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
101.08 KB lrw-r--r-- 2025-08-26 09:08:09
Edit Download
7.01 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.57 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
7.09 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
26.80 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
15.19 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.21 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
6.93 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
11.68 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
6.36 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.99 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
18.98 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.32 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
12.65 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
39.87 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
2.07 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
20.77 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
33.91 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
43.18 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
6.92 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
26.80 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
26.38 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
18.88 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
6.66 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
35.68 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
43.47 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
4.92 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
20.19 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
11.52 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
12.61 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
257 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
60.88 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
17.67 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.07 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
7.11 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
24.29 KB lrw-r--r-- 2025-08-26 09:08:08
Edit Download
11.14 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
109.02 KB lrwxr-xr-x 2025-08-26 08:58:55
Edit Download
22.59 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
27.41 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
19.10 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1003 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
48.96 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
13.03 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
3.00 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
28.80 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
28.06 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
22.91 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
16.27 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
879 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.66 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
78.39 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
6.60 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
23.46 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
18.05 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
17.29 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
19.99 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
21.26 KB lrwxr-xr-x 2018-12-23 21:37:14
Edit Download
5.77 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
6.99 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
78.05 KB lrw-r--r-- 2025-08-26 08:58:55
Edit Download
1.27 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
25.77 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.54 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.21 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.00 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
14.26 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
18.69 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
224.83 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
86.03 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.04 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
24.17 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
29.48 KB lrw-r--r-- 2025-08-26 09:00:17
Edit Download
29.66 KB lrw-r--r-- 2025-08-26 09:06:58
Edit Download
7.04 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.57 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
4.73 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
64 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).