liveBook · Manning

Nogil

As explained in Fast Python: High performance techniques for large datasets

nogil is a Cython function qualifier indicating that a function can execute without holding CPython's global interpreter lock.

nogil is an optional Cython function qualifier marking code that can execute without holding CPython’s global interpreter lock. Cython permits it only after Python objects, calls, and return interactions have been removed; otherwise compilation fails. section 5.4.2 section 5.5

Use nogil after eliminating the remaining Python execution paths:

  • Declare low-level Cython functions with cdef and typed returns. section 5.4.2
  • Operate through typed memoryviews or C-level pointers rather than Python-facing arrays and objects. section 5.4.2 section 5.5
  • Pair the qualifier with a parallel executor when real parallel execution is needed; the annotation alone provides no benefit for the single-threaded stage described here. section 5.4.2 section 5.5

Darkening an image

A Cython image-darkening function using typed memoryviews and nogil reduced runtime to 0.04 seconds, compared with 35 seconds for native Python and 18 seconds for naive Cython. The qualifier enabled future parallelism but did not explain this single-threaded speedup. section 5.4.2

@cython.boundscheck(False)                       #1
cdef void darken_annotated_mv(                   #2
        cnp.uint8_t[:,:,:] image_mv,
        cnp.uint8_t[:,:] darken_filter_mv,
        cnp.uint8_t[:,:] dark_image_mv) nogil:   #3

The compact definition shows nogil attached to a cdef function after typed memoryview parameters, alongside disabled bounds checking. section 5.4.2

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Explains qualifier constraints and surrounding typed-memoryview changes, but defers actual parallel execution to later treatment.
Examples
Uses image darkening, typed pixel pointers, and a NumPy generalized universal function as concrete implementations.
What it assumes
Assumes Cython function declarations, typed memoryviews, C-level pointers, and basic NumPy array types.
What you can do after
Apply it to Python-independent Cython kernels before integrating a parallel executor.
How it connects
Connects nogil to the GIL, Cython functions, typed memoryviews, bounds checking, NumPy universal functions, and parallelism.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.