Threading in Python using multiple cores

xennygrimmato picture xennygrimmato · Apr 29, 2015 · Viewed 7.3k times · Source

As far as I know, Python's threading library uses POSIX threads for threading and it does not run on multiple cores. So is it possible that we implement a multicore threading system for Python threads using Open MP?

Answer

myaut picture myaut · Apr 29, 2015

CPython ("default" Python implementation) is not utilizing multiple cores because of Global Interpreter Lock. So every Python statement has to hold that lock.

But modules that are written in C may release interpreter lock before time-consuming operation. I.e. numpy does that: http://wiki.scipy.org/ParallelProgramming

They have handy example for that:

import numpy as np
import math

def f(x):
    print x
    # This statements hold GIL and cannot be run
    # in two parallel threads 
    y = [1]*10000000
    [math.exp(i) for i in y]

def g(x):
    print x
    # This statements fall to NumPy C code
    # than release GIL and can be multithreaded
    y = np.ones(10000000)
    np.exp(y)

Since OpenMP is also a tool for C, I think that is what you seek for.