Skip to content

Commit

Permalink
Document @cython.with_gil (GH-5522)
Browse files Browse the repository at this point in the history
  • Loading branch information
matusvalo committed Jul 12, 2023
1 parent f4dc8c1 commit 0c6141a
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions docs/src/userguide/nogil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ You can mark a whole function (either a Cython function or an :ref:`external fun
.. tabs::

.. group-tab:: Pure Python

.. code-block:: python
@cython.nogil
Expand All @@ -52,9 +52,9 @@ You can mark a whole function (either a Cython function or an :ref:`external fun
...
.. group-tab:: Cython

.. code-block:: cython
cdef void some_func() noexcept nogil:
....
Expand All @@ -77,37 +77,63 @@ To actually release the GIL you can use context managers
.. tabs::

.. group-tab:: Pure Python

.. code-block:: python
with cython.nogil:
... # some code that runs without the GIL
... # some code that runs without the GIL
with cython.gil:
... # some code that runs with the GIL
... # some more code without the GIL
... # some code that runs with the GIL
... # some more code without the GIL
.. group-tab:: Cython

.. code-block:: cython
with nogil:
... # some code that runs without the GIL
... # some code that runs without the GIL
with gil:
... # some code that runs with the GIL
... # some more code without the GIL
... # some more code without the GIL
The ``with gil`` block is a useful trick to allow a small
chunk of Python code or Python object processing inside a non-GIL block. Try not to use it
too much since there is a cost to waiting for and acquiring the GIL, and because these
blocks cannot run in parallel since all executions require the same lock.

A function may be marked as ``with gil`` to ensure that the
GIL is acquired immediately then calling it. This is currently a Cython-only
feature (no equivalent syntax exists in pure-Python mode)::
A function may be marked as ``with gil`` or decorated with ``@cython.with_gil`` to ensure that the
GIL is acquired immediately when calling it.

.. tabs::

.. group-tab:: Pure Python

.. code-block:: python
@cython.with_gil
@cython.cfunc
def some_func() -> cython.int
...
with cython.nogil:
... # some code that runs without the GIL
some_func() # some_func() will internally acquire the GIL
... # some code that runs without the GIL
some_func() # GIL is already held hence the function does not need to acquire the GIL
.. group-tab:: Cython

.. code-block:: cython
cdef int some_func() with gil:
...
with nogil:
... # some code that runs without the GIL
some_func() # some_func() will internally acquire the GIL
... # some code that runs without the GIL
some_func() # GIL is already held hence the function does not need to acquire the GIL
cdef int some_func() with gil:
...
Conditionally acquiring the GIL
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -117,19 +143,19 @@ This is most often used when working with :ref:`fusedtypes`
.. tabs::

.. group-tab:: Pure Python

.. code-block:: python
with cython.nogil(some_type is not object):
... # some code that runs without the GIL, unless we're processing objects
.. group-tab:: Cython

.. code-block:: cython
with nogil(some_type is not object):
... # some code that runs without the GIL, unless we're processing objects
Exceptions and the GIL
----------------------

Expand Down

0 comments on commit 0c6141a

Please sign in to comment.