mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
Add notes on nogil & reinitialization to the Opt-Out section in Module Isolation HOWTO (GH-134141)
Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
parent
7309eb60c0
commit
1f0a294e8c
1 changed files with 15 additions and 0 deletions
|
|
@ -215,21 +215,36 @@ multiple interpreters correctly. If this is not yet the case for your
|
|||
module, you can explicitly make your module loadable only once per
|
||||
process. For example::
|
||||
|
||||
// A process-wide flag
|
||||
static int loaded = 0;
|
||||
|
||||
// Mutex to provide thread safety (only needed for free-threaded Python)
|
||||
static PyMutex modinit_mutex = {0};
|
||||
|
||||
static int
|
||||
exec_module(PyObject* module)
|
||||
{
|
||||
PyMutex_Lock(&modinit_mutex);
|
||||
if (loaded) {
|
||||
PyMutex_Unlock(&modinit_mutex);
|
||||
PyErr_SetString(PyExc_ImportError,
|
||||
"cannot load module more than once per process");
|
||||
return -1;
|
||||
}
|
||||
loaded = 1;
|
||||
PyMutex_Unlock(&modinit_mutex);
|
||||
// ... rest of initialization
|
||||
}
|
||||
|
||||
|
||||
If your module's :c:member:`PyModuleDef.m_clear` function is able to prepare
|
||||
for future re-initialization, it should clear the ``loaded`` flag.
|
||||
In this case, your module won't support multiple instances existing
|
||||
*concurrently*, but it will, for example, support being loaded after
|
||||
Python runtime shutdown (:c:func:`Py_FinalizeEx`) and re-initialization
|
||||
(:c:func:`Py_Initialize`).
|
||||
|
||||
|
||||
Module State Access from Functions
|
||||
----------------------------------
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue