Understanding gil

56
 Understanding GIL Senthil Kumaran [email protected] Scipy.in 2009 

description

Python global interpreter lock

Transcript of Understanding gil

Page 1: Understanding gil

   

Understanding GIL

Senthil Kumaran

[email protected] 

Scipy.in 2009 

Page 2: Understanding gil

   

How I use python?● Python coredev  stdlib.● At work – Akamai.● Fun.

Page 3: Understanding gil

   

Thanks!● David Beazly – Inside the Python GIL talk.● Pictures are from his slides.

Page 4: Understanding gil

   

Outline of the talk● Threads.● Python Interpreter.● Python Threading Support.● What is GIL.● GIL Behavior.● Newgil in py3.2● Parallelism – Other ways.● etc

Page 5: Understanding gil

   

Threads

● Parallel units of execution within a process.● All threads share the same memory.● Eat and think around the table, sometimes 

sharing the fork and knife. ● Problems with Threading involve the Shared 

Resources and Thread Synchronization

Page 6: Understanding gil

   

Python Programming language● Different Implementations. 

● Cpython●  Jython● IronPython.

● We are going to discuss Cpython implementation.

● Bytecode based.

Page 7: Understanding gil

   

Python Interpretor● Interpreter is a single process.● Within that process, only one thread can be 

interpreting python byte codes at one time. ● If that thread is blocked (waiting or I/O), 

another thread can be run. Some C extensions release GIL so that threads can run concurrently.

Page 8: Understanding gil

   

This is how

Page 9: Understanding gil

   

Python and Threading Support

● A standard library module called threading.● But the execution of threaded programs is 

very basic.● You have to define the synchronization 

mechanism.

Page 10: Understanding gil

   

What is Python Thread?● Python threads are real system threads.

● POSIX threads (pthreads)● Windows threads

● Fully managed by the host operating system.● All the scheduling/threading switching.

● Represent threaded execution of the Python inpterpretor process (written in C).

Page 11: Understanding gil

   

Thread Creation

● Python threads simply execute a “callable”● The run() method of Thread ● Inside, the Python creates a small data 

structure containing some interpreter state.● A new thread (pthread) is lauched,● The thread calls PyEval_CallObject. It is just a 

C function call that runs the Python callable.

Page 12: Understanding gil

   

Thread Specific State● Each thread has its own interpreter specific data 

structure (PyThreadState)● Details of the Thread currently under execution.

● The interpreter has a global variable that simply points to the ThreadState strcture of the currently running thread. 

● /* Python/pystate.c */● Operations in the Interpreter implicitly depend on this 

variable to know the state.

Page 13: Understanding gil

   

What is GIL

● GIL Serializes the access to most part of the interpreter.

● Only one thread is running at a time. ● The GIL ensures that each thread gets 

exclusive access to the Interpreter internals when its running.

Page 14: Understanding gil

   

GIL Behaviour● Its simple. Threads hold the GIL when 

running.● However, they release it when blocking for 

I/O.● It is coperative multitasking.

Page 15: Understanding gil

   

CPU Bound process● To deal with CPU bound threads that never 

perform any I/O, the interpreter periodically performs a “check”.

● By default, every 100 interpreter “ticks”● sys.setcheckinterval ● The check interval is independent of thread 

scheduling.

Page 16: Understanding gil

   

sys.setcheckinterval

Page 17: Understanding gil

   

The Periodic Check● Release and Reacquire GIL.● Signaling the OS to run other thread.● Other threads get to run.. (somewhat )

All of these is for py < 3.2

Page 18: Understanding gil

   

Interpreter ticks● They are not time based ( for < py3.2)● Ticks are uninterruptible● Long Operations can block everything.

Page 19: Understanding gil

   

Thread Scheduling● Interpreter does not do any thread scheduling.● There is no notion of thread priorities, preemption, 

roundrobin scheduling.● All thread scheduling is left to the host operating 

system.● There comes the problem with signals. Only main 

thread handles signals and interpretor cannot schedule it execution, but has to wait for OS to schedule it when the any signal arrives.

Page 20: Understanding gil

   

Interpreter does NO thread scheduling

Page 21: Understanding gil

   

More details on GIL● Its not a simple mutex.● It is some kind of semaphore.● The acquisition and release is based on the 

signals from the OS.● OS does the scheduling based on priorities 

like CPU bound task has low priority and IO bound tasks have higher priority.

Page 22: Understanding gil

   

Multicore GIL contention● CPU bound operations get scheduled 

simultaneous.● And they have a GIL battle in the Interpeter.

Page 23: Understanding gil

   

GIL Contention

Page 24: Understanding gil

   

Newgil implementation● Antoine Pitrou – Oct 2009● Ditch the switching based on tick in the newgil 

implementation. (No Py_Ticker based count)● Use fixed intervals (5 milliseconds) for the main thread to 

release the GIL.● This fixed interval switching has significant benefits on 

thread contentions. ● Forced thread switching.  Improves the thread switch 

latency.

Page 25: Understanding gil

   

ccbench● Pure Python workload (PI Calculation).● C workload which does not release GIL 

(re.search)● C workload which does release GIL.

Page 26: Understanding gil

   

Other Concurrency Options● Practical programming scenarios ( IO and CPU Bound)

● Multi processing.

● C extension that can release GIL.

● Asynchronous event based – Great for Networking applications. 

● Coroutines – python 2.5 where you can pass values to generators.

● Other Python implementations. Jython, IronPython, Unladen Swallow.

Page 27: Understanding gil

   

Thanks● [email protected]● http://uthcode.sarovar.org● Search or David Beazly talk on Inside the 

Python GIL.● Newgil implementation is currently in py3k 

branch (which will become python 3.2, est. by Jun 2010) 

Page 28: Understanding gil

   

Notes● sys.{set,get}checkinterval● gil_locked and gil_mutex● gil_drop_request● gil_cond variable (waiting for interval)● switch_cond  check for gil_last_hold.

Page 29: Understanding gil

   

 

    1

Understanding GIL

Senthil Kumaran

[email protected] 

Scipy.in 2009 

Page 30: Understanding gil

   

 

    2

How I use python?● Python coredev  stdlib.● At work – Akamai.● Fun.

Page 31: Understanding gil

   

 

    3

Thanks!● David Beazly – Inside the Python GIL talk.● Pictures are from his slides.

Page 32: Understanding gil

   

 

    4

Outline of the talk● Threads.● Python Interpreter.● Python Threading Support.● What is GIL.● GIL Behavior.● Newgil in py3.2● Parallelism – Other ways.● etc

Page 33: Understanding gil

   

 

    5

Threads

● Parallel units of execution within a process.● All threads share the same memory.● Eat and think around the table, sometimes 

sharing the fork and knife. ● Problems with Threading involve the Shared 

Resources and Thread Synchronization

Page 34: Understanding gil

   

 

    6

Python Programming language● Different Implementations. 

● Cpython●  Jython● IronPython.

● We are going to discuss Cpython implementation.

● Bytecode based.

Page 35: Understanding gil

   

 

    7

Python Interpretor● Interpreter is a single process.● Within that process, only one thread can be 

interpreting python byte codes at one time. ● If that thread is blocked (waiting or I/O), 

another thread can be run. Some C extensions release GIL so that threads can run concurrently.

Page 36: Understanding gil

   

 

    8

This is how

Page 37: Understanding gil

   

 

    9

Python and Threading Support

● A standard library module called threading.● But the execution of threaded programs is 

very basic.● You have to define the synchronization 

mechanism.

Page 38: Understanding gil

   

 

    10

What is Python Thread?● Python threads are real system threads.

● POSIX threads (pthreads)● Windows threads

● Fully managed by the host operating system.● All the scheduling/threading switching.

● Represent threaded execution of the Python inpterpretor process (written in C).

Page 39: Understanding gil

   

 

    11

Thread Creation

● Python threads simply execute a “callable”● The run() method of Thread ● Inside, the Python creates a small data 

structure containing some interpreter state.● A new thread (pthread) is lauched,● The thread calls PyEval_CallObject. It is just a 

C function call that runs the Python callable.

Page 40: Understanding gil

   

 

    12

Thread Specific State● Each thread has its own interpreter specific data 

structure (PyThreadState)● Details of the Thread currently under execution.

● The interpreter has a global variable that simply points to the ThreadState strcture of the currently running thread. 

● /* Python/pystate.c */● Operations in the Interpreter implicitly depend on this 

variable to know the state.

Page 41: Understanding gil

   

 

    13

What is GIL

● GIL Serializes the access to most part of the interpreter.

● Only one thread is running at a time. ● The GIL ensures that each thread gets 

exclusive access to the Interpreter internals when its running.

Page 42: Understanding gil

   

 

    14

GIL Behaviour● Its simple. Threads hold the GIL when 

running.● However, they release it when blocking for 

I/O.● It is coperative multitasking.

Page 43: Understanding gil

   

 

    15

CPU Bound process● To deal with CPU bound threads that never 

perform any I/O, the interpreter periodically performs a “check”.

● By default, every 100 interpreter “ticks”● sys.setcheckinterval ● The check interval is independent of thread 

scheduling.

Page 44: Understanding gil

   

 

    16

sys.setcheckinterval

Page 45: Understanding gil

   

 

    17

The Periodic Check● Release and Reacquire GIL.● Signaling the OS to run other thread.● Other threads get to run.. (somewhat )

All of these is for py < 3.2

Page 46: Understanding gil

   

 

    18

Interpreter ticks● They are not time based ( for < py3.2)● Ticks are uninterruptible● Long Operations can block everything.

Page 47: Understanding gil

   

 

    19

Thread Scheduling● Interpreter does not do any thread scheduling.● There is no notion of thread priorities, preemption, 

roundrobin scheduling.● All thread scheduling is left to the host operating 

system.● There comes the problem with signals. Only main 

thread handles signals and interpretor cannot schedule it execution, but has to wait for OS to schedule it when the any signal arrives.

Page 48: Understanding gil

   

 

    20

Interpreter does NO thread scheduling

Page 49: Understanding gil

   

 

    21

More details on GIL● Its not a simple mutex.● It is some kind of semaphore.● The acquisition and release is based on the 

signals from the OS.● OS does the scheduling based on priorities 

like CPU bound task has low priority and IO bound tasks have higher priority.

Page 50: Understanding gil

   

 

    22

Multicore GIL contention● CPU bound operations get scheduled 

simultaneous.● And they have a GIL battle in the Interpeter.

Page 51: Understanding gil

   

 

    23

GIL Contention

Page 52: Understanding gil

   

 

    24

Newgil implementation● Antoine Pitrou – Oct 2009● Ditch the switching based on tick in the newgil 

implementation. (No Py_Ticker based count)● Use fixed intervals (5 milliseconds) for the main thread to 

release the GIL.● This fixed interval switching has significant benefits on 

thread contentions. ● Forced thread switching.  Improves the thread switch 

latency.

Page 53: Understanding gil

   

 

    25

ccbench● Pure Python workload (PI Calculation).● C workload which does not release GIL 

(re.search)● C workload which does release GIL.

Page 54: Understanding gil

   

 

    26

Other Concurrency Options● Practical programming scenarios ( IO and CPU Bound)

● Multi processing.

● C extension that can release GIL.

● Asynchronous event based – Great for Networking applications. 

● Coroutines – python 2.5 where you can pass values to generators.

● Other Python implementations. Jython, IronPython, Unladen Swallow.

Page 55: Understanding gil

   

 

    27

Thanks● [email protected]● http://uthcode.sarovar.org● Search or David Beazly talk on Inside the 

Python GIL.● Newgil implementation is currently in py3k 

branch (which will become python 3.2, est. by Jun 2010) 

Page 56: Understanding gil

   

 

    28

Notes● sys.{set,get}checkinterval● gil_locked and gil_mutex● gil_drop_request● gil_cond variable (waiting for interval)● switch_cond  check for gil_last_hold.