7

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?

xennygrimmato
  • 2,646
  • 7
  • 25
  • 47

3 Answers3

5

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.

myaut
  • 11,174
  • 2
  • 30
  • 62
3

multiprocessing:

The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

user3557327
  • 1,109
  • 13
  • 22
0

CPython has a Global Interpreter Lock a.k.a GIL. The GIL ensures that only one thread runs in the interpreter at once. Can your multicore threading system beat the GIL?

More about GIL:

Assem
  • 11,574
  • 5
  • 59
  • 97