# PEP 684: A Per-Interpreter GIL

**URL:** <https://discuss.python.org/t/pep-684-a-per-interpreter-gil/19583>\
**Category:** PEPs\
**Created:** [September 29, 2022, 11:05pm UTC](https://discuss.python.org/t/pep-684-a-per-interpreter-gil/19583 "2022-09-29T23:05:58Z")\
**Posts on this page:** 1\
**Showing post:** 21

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [October 31, 2022, 1:00pm UTC](https://discuss.python.org/t/pep-684-a-per-interpreter-gil/19583/21 "2022-10-31T13:00:09Z")

</div>

> [@eric.snow](#):
>
> Are we okay to require that the “mem” and “object” allocators be thread-safe, whereas currently we say they can rely on the GIL?  
> Can we avoid making extensions opt in to supporting per-interpreter GIL (if they already implement multi-phase init)?

My vote goes to _no_: make 3.12 safe, then remove the limitations.  
For example, `PyMem_SetAllocator` with `PYMEM_DOMAIN_MEM` or `PYMEM_DOMAIN_OBJ` could block creating independent GILs, and new `PyMem_SetGlobalAllocator` could be added.

And, I guess setting memory allocators should be blocked if multiple GILs exist? [Apparently](https://github.com/python/cpython/issues/96997), after Python is initialized, `PyMem_SetAllocator` should be only used only for hooks that wrap the current allocator (is that right @vstinner?), but _creating_ such a hook using `PyMem_GetAllocator` gets you a race condition. IMO the best thing the initial implementation can do is to fall, and leave a better solution for later.

A wrinkle is that `PyMem_SetAllocator` has no way to signal failure – it silently ignores errors. Guess it predates PyStatus?

> [@How to share module state among multiple instances of an extension module?](https://discuss.python.org/t/how-to-share-module-state-among-multiple-instances-of-an-extension-module/20663/1):
>
> External C libraries often use global structures and corresponding initialization procedures which can only be run once per process.

IMO, the solution is to not opt in for now. If synchronization/introspection API is missing, let’s add it after the PEP is in place. (IMO there are many issues in this area – that’s why I’m trying to convince Eric to make the initial implementation safe but limited.)

---

_[View the full topic](https://discuss.python.org/t/pep-684-a-per-interpreter-gil/19583)._
