SlideShare a Scribd company logo
Python threads: Dive into GIL!PyCon India 2011Pune, Sept 16-18Vishal Kanaujia & Chetan Giridhar
Python: A multithreading example
Setting up the context!!Hmm…My threads should be twice as faster on dual cores!57 % dip in Execution Time on dual core!!
Getting into the problem space!!Python v2.7Getting in to the Problem Space!!GIL
Python ThreadsReal system threads (POSIX/ Windows threads)Python VM has no intelligence of thread management No thread priorities, pre-emptionNative operative system supervises thread schedulingPython interpreter just does the per-thread bookkeeping.
What’s wrong with Py threads?Each ‘running’ thread requires exclusive access to data structures in Python interpreterGlobal interpreter lock (GIL) provides the synchronization (bookkeeping) GIL is necessary, mainly because CPython's memory management is not thread-safe.
GIL: Code DetailsA thread create request in Python is just a pthread_create() callThe function Py_Initialize() creates the GILGIL is simply a synchronization primitive, and can be implemented with a semaphore/ mutex.../Python/ceval.cstatic PyThread_type_lockinterpreter_lock = 0; /* This is the GIL */A “runnable” thread acquires this lock and start execution
GIL ManagementHow does Python manages GIL?Python interpreter regularly performs a    check on the running threadAccomplishes thread switching and signal handlingWhat is a “check”?A counter of ticks; ticks decrement as a thread runs
A tick maps to a Python VM’s byte-code instructions
Check interval can be set with sys.setcheckinterval (interval)
Check dictate CPU time-slice available to a threadGIL Management: ImplementationInvolves two global varibales:PyAPI_DATA(volatile int) _Py_Ticker;PyAPI_DATA(int) _Py_CheckInterval;As soon as ticks reach zero:Refill the ticker
active thread releases the GIL
Signals sleeping threads to wake up
Everyone competes for GILFile: ../ceval.cPyEval_EvalFrameEx () {           --_Py_Ticker;  /* Decrement ticker */          /* Refill it */           _Py_Ticker = _Py_CheckInterval;      if (interpreter_lock) {PyThread_release_lock(interpreter_lock);                /* Other threads may run now */PyThread_acquire_lock(interpreter_lock, 1);      }  }
 Two CPU bound threads on single core machineGIL releasedwaiting for GILthread1RunningSuspendedSignals thread2GIL acquiredwaiting for GILthread2SuspendedWakeupRunningtimecheck1check2
GIL impactThere is considerable time lag with Communication (Signaling)Thread wake-upGIL acquisitionGIL Management: Independent of host/native OS schedulingResultSignificant overheadThread waits if GIL in unavailableThreads run sequentially, rather than concurrentlyTry Ctrl + C. Does it stop execution?
Curious case of multicore systemthread1, core0threadthread2, core1time Conflicting goals of OS scheduler and Python interpreter
 Host OS can schedule threads concurrently on multi-core
 GIL battleThe ‘Priority inversion’In a [CPU, I/O]-bound mixed application, I/O bound thread may starve! “cache-hotness” may influence the new GIL owner; usually the recent owner!Preferring CPU thread over I/O threadPython presents a priority inversion on multi-core systems.
Understanding the new story!!Python v3.2Getting in to the Problem Space!!GIL
New GIL: Python v3.2Regular “check” are discontinuedWe have new time-out mechanism.Default time-out= 5msConfigurable through sys.setswitchinterval()For every time-out, the current GIL holder is forced to release GIL It then signals the other waiting threadsWaits for a signal from new GIL owner (acknowledgement).A sleeping thread wakes up, acquires the GIL, and signals the last owner.
Curious Case of Multicore: Python v3.2CPU Thread core0timeWaiting for the GILGIL releasedRunningWaitSuspendedlangissignalTime Outwaiting for GILGIL acquiredSuspendedWakeupRunningCPU Thread core1
Positive impact with new GILBetter GIL arbitrationEnsures that a thread runs only for 5msLess context switching and fewer signalsMulticore perspective: GIL battle eliminated!More responsive threads (fair scheduling)All iz well
I/O threads in PythonAn interesting optimization by interpreterI/O calls are assumed blockingPython I/O extensively exercise this optimization  with file, socket ops (e.g. read, write, send, recv calls)./Python3.2.1/Include/ceval.hPy_BEGIN_ALLOW_THREADS         Do some blocking I/O operation ...Py_END_ALLOW_THREADSI/O thread always releases the GIL
Convoy effect: Fallout of I/O optimizationWhen an I/O thread releases the GIL, another ‘runnable’ CPU bound thread can acquire it (remember we are on multiple cores).It leaves the I/O thread waiting for another time-out (default: 5ms)!Once CPU thread releases GIL, I/O thread acquires and releases it again This cycle goes on => performance suffers 
Convoy “in” effect	Convoy effect- observed in an application comprising I/O-bound  and CPU-bound threadsGIL releasedGIL releasedI/O Thread core0GIL acquiredTime OutRunningWaitRunningWaitSuspendedwaiting for GILGIL acquiredSuspendedRunningWaitSuspendedCPU Thread core1time
Performance measurements!Curious to know how convoy effect translates into performance numbersWe performed following tests with Python3.2:An application with a CPU and a I/O threadExecuted on a dual core machineCPU thread spends less than few seconds (<10s)!
Comparing: Python 2.7 & Python 3.2Performance dip still observed in dual cores 
Getting into the problem space!!Python v2.7 / v3.2Getting in to the Problem Space!!GILSolution space!!
GIL free world: JythonJython is free of GIL It can fully exploit multiple cores, as per our experimentsExperiments with Jython2.5Run with two CPU threads in tandemExperiment shows performance improvement on a multi-core system
Avoiding GIL impact with multiprocessingmultiprocessing — Process-based “threading” interface“multiprocessing” module spawns a new Python interpreter instance for a process.Each process is independent, and GIL is irrelevant; Utilizes multiple cores better than threads.Shares API with “threading” module.Cool! 40 % improvement in Execution Time on dual core!! 
ConclusionMulti-core systems are becoming ubiquitousPython applications should exploit this abundant powerCPython inherently suffers the GIL limitationAn intelligent awareness of Python interpreter behavior is helpful in developing multi-threaded applicationsUnderstand and use 
QuestionsThank you for your time and attention Please share your feedback/ comments/ suggestions to us at:cjgiridhar@gmail.com ,                http://technobeans.comvishalkanaujia@gmail.com,        http://freethreads.wordpress.com
ReferencesUnderstanding the Python GIL, http://dabeaz.com/talks.htmlGlobalInterpreterLock, http://wiki.python.org/moin/GlobalInterpreterLockThread State and the Global Interpreter Lock, http://docs.python.org/c-api/init.html#threadsPython v3.2.2 and v2.7.2 documentation, http://docs.python.org/Concurrency and Python, http://drdobbs.com/open-source/206103078?pgno=3
Backup slides

More Related Content

What's hot (15)

PPTX
Timers
Shivaditya Jatar
 
PPTX
Python multithreading
Janu Jahnavi
 
PDF
Python multithreading
Janu Jahnavi
 
PDF
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
srkedmi
 
PDF
Introduction to Ruby threads
Luong Vo
 
PPTX
Multithreading Design Patterns
PostSharp Technologies
 
PPTX
Multithreading Fundamentals
PostSharp Technologies
 
PPTX
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Muralidharan Deenathayalan
 
PPTX
Overview of python misec - 2-2012
Tazdrumm3r
 
KEY
OpenBSD/sgi SMP implementation for Origin 350
Takuya ASADA
 
PDF
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
Min-Yih Hsu
 
PDF
Leonardo Nve Egea - Playing in a Satellite Environment 1.2
Jim Geovedi
 
PPTX
Essentials of Multithreaded System Programming in C++
Shuo Chen
 
PDF
Kernel Recipes 2014 - kGraft: Live Patching of the Linux Kernel
Anne Nicolas
 
PDF
Kqueue : Generic Event notification
Mahendra M
 
Python multithreading
Janu Jahnavi
 
Python multithreading
Janu Jahnavi
 
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
srkedmi
 
Introduction to Ruby threads
Luong Vo
 
Multithreading Design Patterns
PostSharp Technologies
 
Multithreading Fundamentals
PostSharp Technologies
 
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Muralidharan Deenathayalan
 
Overview of python misec - 2-2012
Tazdrumm3r
 
OpenBSD/sgi SMP implementation for Origin 350
Takuya ASADA
 
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
Min-Yih Hsu
 
Leonardo Nve Egea - Playing in a Satellite Environment 1.2
Jim Geovedi
 
Essentials of Multithreaded System Programming in C++
Shuo Chen
 
Kernel Recipes 2014 - kGraft: Live Patching of the Linux Kernel
Anne Nicolas
 
Kqueue : Generic Event notification
Mahendra M
 

Viewers also liked (9)

PDF
Python OOP Paradigms and Pythonisms
Chandrashekar Babu
 
PDF
Intelligent Thumbnail Selection
Kamil Sindi
 
PDF
Understanding the Python GIL
David Beazley (Dabeaz LLC)
 
PPTX
Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Advance OOP concepts in Python
Sujith Kumar
 
PDF
In Search of the Perfect Global Interpreter Lock
David Beazley (Dabeaz LLC)
 
PDF
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요
Yongho Ha
 
PDF
Python と型ヒント (Type Hints)
Tetsuya Morimoto
 
Python OOP Paradigms and Pythonisms
Chandrashekar Babu
 
Intelligent Thumbnail Selection
Kamil Sindi
 
Understanding the Python GIL
David Beazley (Dabeaz LLC)
 
Object Oriented Programming in Python
Sujith Kumar
 
Advance OOP concepts in Python
Sujith Kumar
 
In Search of the Perfect Global Interpreter Lock
David Beazley (Dabeaz LLC)
 
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요
Yongho Ha
 
Python と型ヒント (Type Hints)
Tetsuya Morimoto
 
Ad

Similar to Pycon11: Python threads: Dive into GIL! (20)

PDF
GIL - Concurrency & Parallelism in Python
Piyus Gupta
 
PPTX
Pyjion - a JIT extension system for CPython
Anthony Shaw
 
PDF
25 must know python for Interview by Tutort Academy
yashikanigam1
 
PDF
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
Linaro
 
PDF
EuroPython 2020 - Speak python with devices
Hua Chu
 
PDF
Multithreaded_Programming_in_Python.pdf
giridharsripathi
 
PDF
Elasticwulf Pycon Talk
Peter Skomoroch
 
PDF
Squeezing Blood From a Stone V1.2
Jen Costillo
 
PDF
PyCon2022 - Building Python Extensions
Henry Schreiner
 
PDF
Scaling Django with gevent
Mahendra M
 
PDF
Not breaking userspace: the evolving Linux ABI
Alison Chaiken
 
PPTX
QEMU - Binary Translation
Jiann-Fuh Liaw
 
PPT
Multicore
Birgit Plötzeneder
 
PDF
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...
Edge AI and Vision Alliance
 
PDF
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Jian-Hong Pan
 
PDF
淺談 Live patching technology
SZ Lin
 
PDF
Challenges in GPU compilers
AnastasiaStulova
 
KEY
SMP implementation for OpenBSD/sgi
Takuya ASADA
 
PDF
Kernel bug hunting
Andrea Righi
 
GIL - Concurrency & Parallelism in Python
Piyus Gupta
 
Pyjion - a JIT extension system for CPython
Anthony Shaw
 
25 must know python for Interview by Tutort Academy
yashikanigam1
 
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
Linaro
 
EuroPython 2020 - Speak python with devices
Hua Chu
 
Multithreaded_Programming_in_Python.pdf
giridharsripathi
 
Elasticwulf Pycon Talk
Peter Skomoroch
 
Squeezing Blood From a Stone V1.2
Jen Costillo
 
PyCon2022 - Building Python Extensions
Henry Schreiner
 
Scaling Django with gevent
Mahendra M
 
Not breaking userspace: the evolving Linux ABI
Alison Chaiken
 
QEMU - Binary Translation
Jiann-Fuh Liaw
 
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...
Edge AI and Vision Alliance
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Jian-Hong Pan
 
淺談 Live patching technology
SZ Lin
 
Challenges in GPU compilers
AnastasiaStulova
 
SMP implementation for OpenBSD/sgi
Takuya ASADA
 
Kernel bug hunting
Andrea Righi
 
Ad

More from Chetan Giridhar (8)

PPTX
Rapid development & integration of real time communication in websites
Chetan Giridhar
 
PPTX
Async programming and python
Chetan Giridhar
 
PPTX
PyCon India 2012: Rapid development of website search in python
Chetan Giridhar
 
PDF
Fuse'ing python for rapid development of storage efficient FS
Chetan Giridhar
 
PDF
Diving into byte code optimization in python
Chetan Giridhar
 
PPTX
Testers in product development code review phase
Chetan Giridhar
 
PDF
Design patterns in python v0.1
Chetan Giridhar
 
PDF
Tutorial on-python-programming
Chetan Giridhar
 
Rapid development & integration of real time communication in websites
Chetan Giridhar
 
Async programming and python
Chetan Giridhar
 
PyCon India 2012: Rapid development of website search in python
Chetan Giridhar
 
Fuse'ing python for rapid development of storage efficient FS
Chetan Giridhar
 
Diving into byte code optimization in python
Chetan Giridhar
 
Testers in product development code review phase
Chetan Giridhar
 
Design patterns in python v0.1
Chetan Giridhar
 
Tutorial on-python-programming
Chetan Giridhar
 

Recently uploaded (20)

PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Biography of Daniel Podor.pdf
Daniel Podor
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 

Pycon11: Python threads: Dive into GIL!

  • 1. Python threads: Dive into GIL!PyCon India 2011Pune, Sept 16-18Vishal Kanaujia & Chetan Giridhar
  • 3. Setting up the context!!Hmm…My threads should be twice as faster on dual cores!57 % dip in Execution Time on dual core!!
  • 4. Getting into the problem space!!Python v2.7Getting in to the Problem Space!!GIL
  • 5. Python ThreadsReal system threads (POSIX/ Windows threads)Python VM has no intelligence of thread management No thread priorities, pre-emptionNative operative system supervises thread schedulingPython interpreter just does the per-thread bookkeeping.
  • 6. What’s wrong with Py threads?Each ‘running’ thread requires exclusive access to data structures in Python interpreterGlobal interpreter lock (GIL) provides the synchronization (bookkeeping) GIL is necessary, mainly because CPython's memory management is not thread-safe.
  • 7. GIL: Code DetailsA thread create request in Python is just a pthread_create() callThe function Py_Initialize() creates the GILGIL is simply a synchronization primitive, and can be implemented with a semaphore/ mutex.../Python/ceval.cstatic PyThread_type_lockinterpreter_lock = 0; /* This is the GIL */A “runnable” thread acquires this lock and start execution
  • 8. GIL ManagementHow does Python manages GIL?Python interpreter regularly performs a check on the running threadAccomplishes thread switching and signal handlingWhat is a “check”?A counter of ticks; ticks decrement as a thread runs
  • 9. A tick maps to a Python VM’s byte-code instructions
  • 10. Check interval can be set with sys.setcheckinterval (interval)
  • 11. Check dictate CPU time-slice available to a threadGIL Management: ImplementationInvolves two global varibales:PyAPI_DATA(volatile int) _Py_Ticker;PyAPI_DATA(int) _Py_CheckInterval;As soon as ticks reach zero:Refill the ticker
  • 14. Everyone competes for GILFile: ../ceval.cPyEval_EvalFrameEx () { --_Py_Ticker; /* Decrement ticker */ /* Refill it */ _Py_Ticker = _Py_CheckInterval; if (interpreter_lock) {PyThread_release_lock(interpreter_lock); /* Other threads may run now */PyThread_acquire_lock(interpreter_lock, 1); } }
  • 15. Two CPU bound threads on single core machineGIL releasedwaiting for GILthread1RunningSuspendedSignals thread2GIL acquiredwaiting for GILthread2SuspendedWakeupRunningtimecheck1check2
  • 16. GIL impactThere is considerable time lag with Communication (Signaling)Thread wake-upGIL acquisitionGIL Management: Independent of host/native OS schedulingResultSignificant overheadThread waits if GIL in unavailableThreads run sequentially, rather than concurrentlyTry Ctrl + C. Does it stop execution?
  • 17. Curious case of multicore systemthread1, core0threadthread2, core1time Conflicting goals of OS scheduler and Python interpreter
  • 18. Host OS can schedule threads concurrently on multi-core
  • 19. GIL battleThe ‘Priority inversion’In a [CPU, I/O]-bound mixed application, I/O bound thread may starve! “cache-hotness” may influence the new GIL owner; usually the recent owner!Preferring CPU thread over I/O threadPython presents a priority inversion on multi-core systems.
  • 20. Understanding the new story!!Python v3.2Getting in to the Problem Space!!GIL
  • 21. New GIL: Python v3.2Regular “check” are discontinuedWe have new time-out mechanism.Default time-out= 5msConfigurable through sys.setswitchinterval()For every time-out, the current GIL holder is forced to release GIL It then signals the other waiting threadsWaits for a signal from new GIL owner (acknowledgement).A sleeping thread wakes up, acquires the GIL, and signals the last owner.
  • 22. Curious Case of Multicore: Python v3.2CPU Thread core0timeWaiting for the GILGIL releasedRunningWaitSuspendedlangissignalTime Outwaiting for GILGIL acquiredSuspendedWakeupRunningCPU Thread core1
  • 23. Positive impact with new GILBetter GIL arbitrationEnsures that a thread runs only for 5msLess context switching and fewer signalsMulticore perspective: GIL battle eliminated!More responsive threads (fair scheduling)All iz well
  • 24. I/O threads in PythonAn interesting optimization by interpreterI/O calls are assumed blockingPython I/O extensively exercise this optimization with file, socket ops (e.g. read, write, send, recv calls)./Python3.2.1/Include/ceval.hPy_BEGIN_ALLOW_THREADS Do some blocking I/O operation ...Py_END_ALLOW_THREADSI/O thread always releases the GIL
  • 25. Convoy effect: Fallout of I/O optimizationWhen an I/O thread releases the GIL, another ‘runnable’ CPU bound thread can acquire it (remember we are on multiple cores).It leaves the I/O thread waiting for another time-out (default: 5ms)!Once CPU thread releases GIL, I/O thread acquires and releases it again This cycle goes on => performance suffers 
  • 26. Convoy “in” effect Convoy effect- observed in an application comprising I/O-bound and CPU-bound threadsGIL releasedGIL releasedI/O Thread core0GIL acquiredTime OutRunningWaitRunningWaitSuspendedwaiting for GILGIL acquiredSuspendedRunningWaitSuspendedCPU Thread core1time
  • 27. Performance measurements!Curious to know how convoy effect translates into performance numbersWe performed following tests with Python3.2:An application with a CPU and a I/O threadExecuted on a dual core machineCPU thread spends less than few seconds (<10s)!
  • 28. Comparing: Python 2.7 & Python 3.2Performance dip still observed in dual cores 
  • 29. Getting into the problem space!!Python v2.7 / v3.2Getting in to the Problem Space!!GILSolution space!!
  • 30. GIL free world: JythonJython is free of GIL It can fully exploit multiple cores, as per our experimentsExperiments with Jython2.5Run with two CPU threads in tandemExperiment shows performance improvement on a multi-core system
  • 31. Avoiding GIL impact with multiprocessingmultiprocessing — Process-based “threading” interface“multiprocessing” module spawns a new Python interpreter instance for a process.Each process is independent, and GIL is irrelevant; Utilizes multiple cores better than threads.Shares API with “threading” module.Cool! 40 % improvement in Execution Time on dual core!! 
  • 32. ConclusionMulti-core systems are becoming ubiquitousPython applications should exploit this abundant powerCPython inherently suffers the GIL limitationAn intelligent awareness of Python interpreter behavior is helpful in developing multi-threaded applicationsUnderstand and use 
  • 33. QuestionsThank you for your time and attention Please share your feedback/ comments/ suggestions to us at:[email protected] , http://[email protected], http://freethreads.wordpress.com
  • 34. ReferencesUnderstanding the Python GIL, http://dabeaz.com/talks.htmlGlobalInterpreterLock, http://wiki.python.org/moin/GlobalInterpreterLockThread State and the Global Interpreter Lock, http://docs.python.org/c-api/init.html#threadsPython v3.2.2 and v2.7.2 documentation, http://docs.python.org/Concurrency and Python, http://drdobbs.com/open-source/206103078?pgno=3
  • 36. Python: GILA thread needs GIL before updating Python objects, calling C/Python API functionsConcurrency is emulated with regular ‘checks’ to switch threadsApplicable to only CPU bound threadA blocking I/O operation implies relinquishing the GIL./Python2.7.5/Include/ceval.hPy_BEGIN_ALLOW_THREADS Do some blocking I/O operation ...Py_END_ALLOW_THREADSPython file I/O extensively exercise this optimization
  • 37. GIL: InternalsThe function Py_Initialize() creates the GILA thread create request in Python is just a pthread_create() call../Python/ceval.cstatic PyThread_type_lock interpreter_lock = 0; /* This is the GIL */o) thread_PyThread_start_new_thread: we call it for "each" user defined thread. calls PyEval_InitThreads() -> PyThread_acquire_lock() {}
  • 38. GIL: in actionEach CPU bound thread requires GIL
  • 39. ‘ticks count’ determine duration of GIL hold
  • 41. We keep a list of Python threads and each thread-state has its tick_counter value
  • 42. As soon as tick decrements to zero, the thread release the GIL.GIL: Detailsthread_PyThread_start_new_thread() ->void PyEval_InitThreads(void){ if (interpreter_lock) return; interpreter_lock = PyThread_allocate_lock(); PyThread_acquire_lock(interpreter_lock, 1); main_thread = PyThread_get_thread_ident();}
  • 43. Convoy effect: Python v2?Convoy effect holds true for Python v2 alsoThe smaller interval of ‘check’ saves the day!I/O threads don’t have to wait for a longer time (5 m) for CPU threads to finishShould choose the setswitchinterval() wiselyThe effect is not so visible in Python v2.0
  • 44. Stackless PythonA different way of creating threads: Microthreads!No improvement from multi-core perspectiveRound-robin scheduling for “tasklets”Sequential execution 

Editor's Notes

  • #16: Okokokokkokokokok