Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update dotnet.rst
  • Loading branch information
filmor authored Jan 28, 2023
commit 6fb11e14529e4106f01980e85f76d93f2aeb19d9
22 changes: 19 additions & 3 deletions doc/source/dotnet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,32 @@ Before interacting with any of the objects or APIs provided by the
``Python.Runtime`` namespace, calling code must have acquired the Python
global interpreter lock by ``using'' ``Py.GIL()``. The only exception to
this rule is the ``PythonEngine.Initialize`` method, which may be called
at startup without having acquired the GIL.

A ``using`` statement may be used to acquire and release the GIL:
at startup without having acquired the GIL. The GIL is released again
by disposing the return value of `Py.GIL()`:

.. code:: csharp

using (Py.GIL())
{
PythonEngine.Exec("doStuff()");
}

// or
{
using var _ = Py.GIL()
PythonEngine.Exec("doStuff()");
}

// or
var gil = Py.GIL();
try
{
PythonEngine.Exec("doStuff()");
}
finally
{
gil.Dispose();
}

The ``Py.GIL()'' object is a thin wrapper over the unmanaged
``PyGILState_Ensure`` (on construction) and ``PyGILState_Release`` (on
Expand Down