# Python3.13 without GIL thread-safe for all thread-unsafe code?

**URL:** <https://discuss.python.org/t/python3-13-without-gil-thread-safe-for-all-thread-unsafe-code/66622>\
**Category:** Python Help\
**Created:** [October 4, 2024, 10:22am UTC](https://discuss.python.org/t/python3-13-without-gil-thread-safe-for-all-thread-unsafe-code/66622 "2024-10-04T10:22:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArtemIsmagilov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/artemismagilov/32/22941_2.png) [@ArtemIsmagilov](https://discuss.python.org/u/ArtemIsmagilov)\
**Post date:** [October 4, 2024, 10:22am UTC](https://discuss.python.org/t/python3-13-without-gil-thread-safe-for-all-thread-unsafe-code/66622/1 "2024-10-04T10:22:13Z")

</div>

Many Python libraries strictly specify that, for example, some functionality is not thread-safe. Here is one example pypi cachetools  
The authors write - Note Please be aware that all these classes are not thread-safe. Access to a shared cache from multiple threads must be properly synchronized, e.g. by using one of the memoizing decorators with a suitable lock object.

There is also such a warning for the standard library [locale — Internationalization services — Python 3.12.7 documentation](https://docs.python.org/3/library/locale.html)

setlocale() is not thread-safe on most systems.

The same problem was, for example, in [sched — Event scheduler — Python 3.12.7 documentation](https://docs.python.org/3/library/sched.html)

However, now this battery is thread-safe.

Hence the question. After removing the GIL from python3.13 and adding mutexes, are all programs now thread-safe by default and do not need synchronization? This also applies to the asyncio module and all the others that also have many warnings about thread-unsafety in multithreaded applications.  
Then why would all the locks (Lock, RLock, and so on) be needed in the threading module?

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [October 4, 2024, 11:44am UTC](https://discuss.python.org/t/python3-13-without-gil-thread-safe-for-all-thread-unsafe-code/66622/2 "2024-10-04T11:44:35Z")

</div>

> [@ArtemIsmagilov](#):
>
> After removing the GIL from python3.13 and adding mutexes, are all programs now thread-safe by default and do not need synchronization?

No, those mutexes have no effect on these situations. The primary effect of the mutexes is that individual python objects don’t get corrupted on a C level if multiple threads access them.

However, for e.g. cachetools that doesn’t mean the python code is synced up in the correct way. Multiple threads might still both see the value missing and start calculating.

---

<div class="post-metadata">

**Author:** ![ArtemIsmagilov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/artemismagilov/32/22941_2.png) [@ArtemIsmagilov](https://discuss.python.org/u/ArtemIsmagilov)\
**Post date:** [October 4, 2024, 12:01pm UTC](https://discuss.python.org/t/python3-13-without-gil-thread-safe-for-all-thread-unsafe-code/66622/3 "2024-10-04T12:01:42Z")

</div>

> [@What is Thread Safety as applied to Python?](https://discuss.python.org/t/what-is-thread-safety-as-applied-to-python/13685/4):
>
> Removing the GIL does not make functions thread-safe. Thread-safety relates to this question: If you have a data structure (say, an object, or a list) that is being modified in one thread, can another thread come along and modify it right in the middle of that calculation? Let’s say you have a global variable x which equals 0, and two different threads attempt to increment it using this: x += 1 If incrementing is thread-safe, then you are guaranteed that the result can only be x=2 after the…

---

<div class="post-metadata">

**Author:** ![joernheissler](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/joernheissler/32/23025_2.png) [@joernheissler](https://discuss.python.org/u/joernheissler)\
**Post date:** [October 8, 2024, 2:12pm UTC](https://discuss.python.org/t/python3-13-without-gil-thread-safe-for-all-thread-unsafe-code/66622/4 "2024-10-08T14:12:45Z")

</div>

I had a look at PEP 703 and it’s unclear to me which operations are now thread safe and which are not.  
E.g. is assigning to a dict thread-safe, or do I need to hold a Lock?

```python
from threading import Thread

class Foo:
  def _t(self, name):
    self._d[name] = name + name

  def run(self):
    self._d = {}
    threads = [Thread(target=self._t, args=(name,))
               for name in ["foo", "bar", "baz"]]
    for thread in threads:
      thread.start()
    for thread in threads:
      thread.join()
    print(self._d)

Foo().run()

```

> [@MegaIng](#):
>
> The primary effect of the mutexes is that individual python objects don’t get corrupted on a C level if multiple threads access them.

That could mean at least two things:

- Above code works as expected, i.e. the program will print `{'foo': 'foofoo', 'bar': 'barbar', 'baz': 'bazbaz'}` (possibly in different order).
- The program doesn’t crash but might print something else, e.g. fewer keys.

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [October 8, 2024, 2:14pm UTC](https://discuss.python.org/t/python3-13-without-gil-thread-safe-for-all-thread-unsafe-code/66622/5 "2024-10-08T14:14:45Z")

</div>

Since you are only writing, it should follow case 1, i.e. work as expected.
