Gotcha 1: Parallelization

Gotcha 1: Parallelization#

Execution Behavior of Compiled Extensions:

  • Python scripts act as an orchestration facade.

  • Underlying libraries (e.g., NumPy) can save state, explicitly release the GIL, and delegate tasks to compiled C/Fortran code.

  • The compiled code utilizes true multi-core parallelism.

import numpy as np

size = 4000
arr1 = np.random.rand(size, size)
arr2 = np.random.rand(size, size)

for _ in range(100):
    np.dot(arr1, arr2)

⚠️ The Oversubscription Risk:
Naively parallelizing a Python loop over operations that are already internally parallelized leads to severe thread contention and drastic performance degradation.*