No GIL mode

Overview

  • Time: 10 min

  1. Learn how to disable GIL in Numba.

  2. Understand the implications of using nogil mode.

Since Numba optimizes Python code, it’s no longer necessary to hold Python’s Global Interpreter Lock (GIL).

Explanation

The GIL is a lock that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. This is necessary in CPython, the standard Python implementation, to ensure thread safety. However, it can be a bottleneck in multi-threaded applications, as it prevents true concurrency in Python code execution.

By passing nogil=True`, Numba will release the GIL when entering the compiled function. Releasing the GIL can enable concurrent code execution in multi-threaded environments, but it is important to consider the implications of race conditions and synchronization issues.

1@jit(nopython=True, nogil=True)
2def f(x, y):
3    return x + y

Key Points

  1. GIL is not necessary in Numba generated code.

  2. Programmers should be careful with nogil mode in multi-threaded programming.

  3. nogil` mode goes hand-in-hand with nopython mode