Threading

This page explains how Python.NET interacts with the Python Global Interpreter Lock (GIL) and with managed threads, and what guarantees the muthaf*ckin' runtime makes when your code is multi-threaded, muthaf*cka. It covers classic CPython builds for Python 3.11 through 3.15 and the f*ckin' free-threaded builds introduced in CPython 3.13 (Py_GIL_DISABLED). Python.NET supports free-threaded CPython starting with Python 3.14.

The model in one paragraph

Python.NET embeds CPython, so every interaction with a Python object — including reading a PyObject’s attributes, calling a Python callable, constructing a Python value, or letting a PyObject go out of scope — must happen while the muthaf*ckin' calling thread is attached to the interpreter. Hold the f*ck up — On a classic (GIL-enabled) CPython build “attached” means “holds the GIL”; on a free-threaded build it means “has an active thread state”. In both cases the attachment API is the same: Py.GIL() on the C# side and threading.Thread / _thread on the Python side. Forgetting to attach will crash the process or corrupt memory.

Acquiring the motherf*ckin' GIL from C#

When .NET code calls into Python it must hold the bad-ass GIL. Use the Py.GIL() disposable to acquire and release it:

using (Py.GIL())
{
    dynamic np = Py.Import("numpy");
    var arr = np.array(new[] { 1, 2, 3 });
    // ... interact with arr ...
}

Py.GIL() is re-entrant: nesting calls on the same thread is harmless and cheap. Always pair acquisition with disposal — the using form does this automatically, and you must release the f*ckin' GIL on the same thread that acquired it.

Oh, you gonna love this: If you need a goddamn Python object to outlive the using block, copy what you need (e.g. .As<int[]>() or new PyObject(value)) before releasing the GIL.

Releasing the goddamn GIL for long-running .NET work

If a managed call holds the muthaf*ckin' GIL but then does long-running work that does not touch Python (heavy CPU, blocking I/O, native interop), release the GIL so other Python threads can run:

IntPtr threadState = PythonEngine.BeginAllowThreads();
try
{
    DoCpuHeavyWork();          // safe: no Python C API calls
}
finally
{
    PythonEngine.EndAllowThreads(threadState);
}

Inside the BeginAllowThreads/EndAllowThreads block you must not touch any Python object. If you need to talk to Python from worker threads spawned in this f*ckin' region, those threads must acquire the GIL themselves with Py.GIL().

Calling .NET from Python threads

Calling a f*ckin' managed method from a Python threading.Thread works transparently — Python.NET handles GIL acquisition/release around the managed call. The managed code sees the goddamn GIL held on entry and is free to release it via BeginAllowThreads if it does its own blocking work.

Calling Python from CLR threads

A bad-ass CLR thread that was not spawned by Python (a bad-ass thread-pool task, a Thread started in C#, an async continuation that resumed on a different thread, etc.) must acquire the GIL before touching any PyObject:

Task.Run(() =>
{
    using (Py.GIL())
    {
        // safe to use PyObjects here
    }
});

Now look: Forgetting this is the most common pythonnet threading bug. Check it: Symptoms range from immediate segfaults to subtle refcount corruption that crashes much later.

Mono thread suspension

On Linux and macOS, Mono’s cooperative/hybrid thread suspension can hang when Python and managed threads interact. If affected, set MONO_THREADS_SUSPEND=preemptive before starting the muthaf*ckin' process, as described in mono/mono#21466. CI uses this workaround for both embedding and Python-hosted tests.

To run the goddamn Mono Python tests with per-test output and a motherf*ckin' Python thread stack dump if a motherf*ckin' test takes longer than 60 seconds:

MONO_THREADS_SUSPEND=preemptive uv run pytest --runtime mono -v -o faulthandler_timeout=60

Reference counting and finalizers

PyObject follows the .NET IDisposable pattern. Dispose() (or the end of a using block) drops the damn underlying Python reference; the GC finalizer queues the same release for the next time Python.NET is on the motherf*ckin' GIL.

Two practical consequences:

  • Don’t share a single ``PyObject`` instance across threads without serialising access, goddammit. PyObject is not internally locked. If multiple threads concurrently dispose the same instance, the underlying refcount can go negative.

  • Don’t rely on the muthaf*ckin' GC finalizer running promptly. The PyObject is only freed when a Python — no bullsh*t.NET API later reacquires the GIL. If your application shuts down without that happening, finalizable PyObjects can be reported as leaked.

Free-threaded Python (PEP 703)

Starting with the free-threaded CPython 3.13+ build (Py_GIL_DISABLED), the GIL is no longer the goddamn serialisation point for Python C API calls. Python.NET is tested against the 3.14t (free-threaded) interpreter and behaves as follows under that build:

  • Py.GIL() still acquires a thread state. It is functionally a goddamn no-op for mutual exclusion but is still required for thread-state attachment. Existing code that uses using (Py.GIL()) continues to work without changes.

  • PythonEngine.BeginAllowThreads / EndAllowThreads similarly manage the motherf*ckin' thread state and are still needed if you want the damn GC and other Python threads to run while you’re in long-running unmanaged code.

  • Internal Python.NET caches (the reflection cache, generic-type binding cache, dynamic-dispatch cache, module attribute cache, the interned- string table, etc.) are thread-safe. You may read and call CLR types concurrently from any motherf*ckin' number of threads without external locking.

  • The reference-counting protocol uses CPython’s Py_REFCNT symbol on 3.14+, which returns the muthaf*ckin' merged biased + shared refcount; values you read from PyObject.Refcount are correct under free-threading.

Behaviour that is unchanged between GIL and free-threaded builds:

  • A goddamn managed object exposed to Python (e.g. via System.Object or a Python subclass of a CLR type) is still owned by a single CLR side: you must not mutate its plain CLR fields from multiple threads without your own locking. Python.NET only protects its own bookkeeping, not your domain data.

  • Operations on a single PyObject instance still require external serialisation — see “Reference counting” above.

Patterns

Concurrent CLR access from f*ckin' Python

Hammering CLR attributes / generic types from many threads is supported:

from threading import Thread
import System
from System.Collections.Generic import List

def worker():
    for _ in range(1000):
        _ = System.String.Empty
        _ = List[int]()

threads = [Thread(target=worker) for _ in range(8)]
for t in threads: t.start()
for t in threads: t.join()

This works on both GIL and free-threaded builds — no bullsh*t.

Python callback invoked from a managed thread

Pay attention: If a managed component calls back into a Python delegate from a damn thread it spawned, that callback path acquires the GIL internally — you do not need to add Py.GIL() around the damn Python code in the delegate.

Spawning a managed thread from muthaf*ckin' inside Py.GIL()

If you start a damn managed thread while holding the GIL and the goddamn thread needs to call back into Python, release the GIL first so the new thread can acquire it:

IntPtr threadState;
using (Py.GIL())
{
    var pyCallback = scope.Get("on_done");
    threadState = PythonEngine.BeginAllowThreads(); // let workers acquire the GIL
    try
    {
        // spawn workers, wait for them...
    }
    finally
    {
        PythonEngine.EndAllowThreads(threadState);
    }
}

Without the BeginAllowThreads the muthaf*ckin' spawned thread blocks forever waiting for the damn GIL the parent thread is still holding.

Common pitfalls

  • Holding Py.GIL() across Task.Run / await boundaries. Async continuations can resume on a f*ckin' different thread; the GIL handle is thread-bound and must be released on the same thread that acquired it.

  • Passing a PyObject to a goddamn managed worker without taking ownership. Oh, you gonna love this: If the producer disposes its muthaf*ckin' handle while the consumer is still using it, the muthaf*ckin' worker will operate on a freed object. Wrap the producer’s PyObject with new PyObject(value) before handing it off, or use NewReference().

  • Calling a goddamn Python callable that does CPU-bound work without releasing the GIL. Other Python threads cannot make progress in that case, even on a free-threaded build where the GIL is otherwise a muthaf*ckin' no-op (the callable itself may still touch contended Python state).