SlideShare uma empresa Scribd logo
1 de 35
Python threads: Dive into GIL! PyCon India 2011 Pune, Sept 16-18 Vishal 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.7 Getting in to the Problem Space!! GIL
Python Threads Real system threads (POSIX/ Windows threads) Python VM has no intelligence of thread management  No thread priorities, pre-emption Native operative system supervises thread scheduling Python interpreter just does the per-thread bookkeeping.
What’s wrong with Py threads? Each ‘running’ thread requires exclusive access to data structures in Python interpreter Global interpreter lock (GIL) provides the synchronization (bookkeeping)  GIL is necessary, mainly because CPython's memory management is not thread-safe.
GIL: Code Details A thread create request in Python is just a pthread_create() call The function Py_Initialize() creates the GIL GIL is simply a synchronization primitive, and can be implemented with a semaphore/ mutex. ../Python/ceval.c static PyThread_type_lockinterpreter_lock = 0; /* This is the GIL */ A “runnable” thread acquires this lock and start execution
GIL Management How does Python manages GIL? Python interpreter regularly performs a     check on the running thread Accomplishes thread switching and signal handling What is a “check”? ,[object Object]
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 thread,[object Object]
active thread releases the GIL
Signals sleeping threads to wake up
Everyone competes for GILFile: ../ceval.c PyEval_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 machine GIL released waiting for GIL thread1 Running Suspended Signals thread2 GIL acquired waiting for GIL thread2 Suspended Wakeup Running time check1 check2
GIL impact There is considerable time lag with  Communication (Signaling) Thread wake-up GIL acquisition GIL Management: Independent of host/native OS scheduling Result Significant overhead Thread waits if GIL in unavailable Threads run sequentially, rather than concurrently Try Ctrl + C. Does it stop execution?
Curious case of multicore system thread1, core0 thread thread2, core1 time ,[object Object]
 Host OS can schedule threads concurrently on multi-core
 GIL battle,[object Object]
Understanding the new story!! Python v3.2 Getting in to the Problem Space!! GIL
New GIL: Python v3.2 Regular “check” are discontinued We have new time-out mechanism. Default time-out= 5ms Configurable through sys.setswitchinterval() For every time-out, the current GIL holder is forced to release GIL  It then signals the other waiting threads Waits 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.2 CPU Thread core0 time Waiting for the GIL GIL released Running Wait Suspended langis signal Time Out waiting for GIL GIL acquired Suspended Wakeup Running CPU Thread core1
Positive impact with new GIL Better GIL arbitration Ensures that a thread runs only for 5ms Less context switching and fewer signals Multicore perspective: GIL battle eliminated! More responsive threads (fair scheduling) All iz well
I/O threads in Python An interesting optimization by interpreter I/O calls are assumed blocking Python I/O extensively exercise this optimization  with file, socket ops (e.g. read, write, send, recv calls) ./Python3.2.1/Include/ceval.h Py_BEGIN_ALLOW_THREADS          Do some blocking I/O operation ... Py_END_ALLOW_THREADS I/O thread always releases the GIL
Convoy effect: Fallout of I/O optimization When 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 threads GIL released GIL released I/O Thread core0 GIL acquired Time Out Running Wait Running Wait Suspended waiting for GIL GIL acquired Suspended Running Wait Suspended CPU Thread core1 time
Performance measurements! Curious to know how convoy effect translates into performance numbers We performed following tests with Python3.2: An application with a CPU and a I/O thread Executed on a dual core machine CPU thread spends less than few seconds (<10s)!
Comparing: Python 2.7 & Python 3.2 Performance dip still observed in dual cores 
Getting into the problem space!! Python v2.7 / v3.2 Getting in to the Problem Space!! GIL Solution  space!!
GIL free world: Jython Jython is free of GIL  It can fully exploit multiple cores, as per our experiments Experiments with Jython2.5 Run with two CPU threads in tandem Experiment shows performance improvement on a multi-core system
Avoiding GIL impact with multiprocessing multiprocessing — 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!! 
Conclusion Multi-core systems are becoming ubiquitous Python applications should exploit this abundant power CPython inherently suffers the GIL limitation An intelligent awareness of Python interpreter behavior is helpful in developing multi-threaded applications Understand and use 
Questions Thank you for your time and attention  Please share your feedback/ comments/ suggestions to us at: cjgiridhar@gmail.com ,                http://technobeans.com vishalkanaujia@gmail.com,        http://freethreads.wordpress.com
References Understanding the Python GIL, http://dabeaz.com/talks.html GlobalInterpreterLock, http://wiki.python.org/moin/GlobalInterpreterLock Thread State and the Global Interpreter Lock, http://docs.python.org/c-api/init.html#threads Python 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

Mais conteúdo relacionado

Mais procurados

Python multithreading
Python multithreadingPython multithreading
Python multithreadingJanu Jahnavi
 
Python multithreading
Python multithreadingPython multithreading
Python multithreadingJanu Jahnavi
 
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devicessrkedmi
 
Introduction to Ruby threads
Introduction to Ruby threadsIntroduction to Ruby threads
Introduction to Ruby threadsLuong Vo
 
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning StudioIntroduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning StudioMuralidharan Deenathayalan
 
Overview of python misec - 2-2012
Overview of python   misec - 2-2012Overview of python   misec - 2-2012
Overview of python misec - 2-2012Tazdrumm3r
 
OpenBSD/sgi SMP implementation for Origin 350
OpenBSD/sgi SMP implementation for Origin 350OpenBSD/sgi SMP implementation for Origin 350
OpenBSD/sgi SMP implementation for Origin 350Takuya ASADA
 
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
[COSCUP 2021] LLVM Project: The Good, The Bad, and The UglyMin-Yih Hsu
 
Leonardo Nve Egea - Playing in a Satellite Environment 1.2
Leonardo Nve Egea - Playing in a Satellite Environment 1.2Leonardo Nve Egea - Playing in a Satellite Environment 1.2
Leonardo Nve Egea - Playing in a Satellite Environment 1.2Jim Geovedi
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Shuo Chen
 
Kernel Recipes 2014 - kGraft: Live Patching of the Linux Kernel
Kernel Recipes 2014 - kGraft: Live Patching of the Linux KernelKernel Recipes 2014 - kGraft: Live Patching of the Linux Kernel
Kernel Recipes 2014 - kGraft: Live Patching of the Linux KernelAnne Nicolas
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notificationMahendra M
 

Mais procurados (15)

Timers
TimersTimers
Timers
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
 
Introduction to Ruby threads
Introduction to Ruby threadsIntroduction to Ruby threads
Introduction to Ruby threads
 
Multithreading Design Patterns
Multithreading Design PatternsMultithreading Design Patterns
Multithreading Design Patterns
 
Multithreading Fundamentals
Multithreading FundamentalsMultithreading Fundamentals
Multithreading Fundamentals
 
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning StudioIntroduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
 
Overview of python misec - 2-2012
Overview of python   misec - 2-2012Overview of python   misec - 2-2012
Overview of python misec - 2-2012
 
OpenBSD/sgi SMP implementation for Origin 350
OpenBSD/sgi SMP implementation for Origin 350OpenBSD/sgi SMP implementation for Origin 350
OpenBSD/sgi SMP implementation for Origin 350
 
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
 
Leonardo Nve Egea - Playing in a Satellite Environment 1.2
Leonardo Nve Egea - Playing in a Satellite Environment 1.2Leonardo Nve Egea - Playing in a Satellite Environment 1.2
Leonardo Nve Egea - Playing in a Satellite Environment 1.2
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
Kernel Recipes 2014 - kGraft: Live Patching of the Linux Kernel
Kernel Recipes 2014 - kGraft: Live Patching of the Linux KernelKernel Recipes 2014 - kGraft: Live Patching of the Linux Kernel
Kernel Recipes 2014 - kGraft: Live Patching of the Linux Kernel
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notification
 

Destaque

Python OOP Paradigms and Pythonisms
Python OOP Paradigms and  PythonismsPython OOP Paradigms and  Pythonisms
Python OOP Paradigms and PythonismsChandrashekar Babu
 
Intelligent Thumbnail Selection
Intelligent Thumbnail SelectionIntelligent Thumbnail Selection
Intelligent Thumbnail SelectionKamil Sindi
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in PythonSujith Kumar
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in PythonSujith Kumar
 
In Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockIn Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockDavid Beazley (Dabeaz LLC)
 
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요Yongho Ha
 
Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)Tetsuya Morimoto
 

Destaque (9)

Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Python OOP Paradigms and Pythonisms
Python OOP Paradigms and  PythonismsPython OOP Paradigms and  Pythonisms
Python OOP Paradigms and Pythonisms
 
Intelligent Thumbnail Selection
Intelligent Thumbnail SelectionIntelligent Thumbnail Selection
Intelligent Thumbnail Selection
 
Understanding the Python GIL
Understanding the Python GILUnderstanding the Python GIL
Understanding the Python GIL
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
In Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockIn Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter Lock
 
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요
2011 H3 컨퍼런스-파이썬으로 클라우드 하고 싶어요
 
Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)Python と型ヒント (Type Hints)
Python と型ヒント (Type Hints)
 

Semelhante a Pycon11: Python threads: Dive into GIL!

GIL - Concurrency & Parallelism in Python
GIL - Concurrency & Parallelism in PythonGIL - Concurrency & Parallelism in Python
GIL - Concurrency & Parallelism in PythonPiyus Gupta
 
Pyjion - a JIT extension system for CPython
Pyjion - a JIT extension system for CPythonPyjion - a JIT extension system for CPython
Pyjion - a JIT extension system for CPythonAnthony Shaw
 
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMUSFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMULinaro
 
EuroPython 2020 - Speak python with devices
EuroPython 2020 - Speak python with devicesEuroPython 2020 - Speak python with devices
EuroPython 2020 - Speak python with devicesHua Chu
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfgiridharsripathi
 
Squeezing Blood From a Stone V1.2
Squeezing Blood From a Stone V1.2Squeezing Blood From a Stone V1.2
Squeezing Blood From a Stone V1.2Jen Costillo
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsHenry Schreiner
 
Scaling Django with gevent
Scaling Django with geventScaling Django with gevent
Scaling Django with geventMahendra M
 
Not breaking userspace: the evolving Linux ABI
Not breaking userspace: the evolving Linux ABINot breaking userspace: the evolving Linux ABI
Not breaking userspace: the evolving Linux ABIAlison Chaiken
 
QEMU - Binary Translation
QEMU - Binary Translation QEMU - Binary Translation
QEMU - Binary Translation Jiann-Fuh Liaw
 
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...Edge AI and Vision Alliance
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Jian-Hong Pan
 
淺談 Live patching technology
淺談 Live patching technology淺談 Live patching technology
淺談 Live patching technologySZ Lin
 
Challenges in GPU compilers
Challenges in GPU compilersChallenges in GPU compilers
Challenges in GPU compilersAnastasiaStulova
 
SMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiSMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiTakuya ASADA
 
Kernel bug hunting
Kernel bug huntingKernel bug hunting
Kernel bug huntingAndrea Righi
 
Kubernetes Me this Batman
Kubernetes Me this BatmanKubernetes Me this Batman
Kubernetes Me this BatmanSonatype
 

Semelhante a Pycon11: Python threads: Dive into GIL! (20)

GIL - Concurrency & Parallelism in Python
GIL - Concurrency & Parallelism in PythonGIL - Concurrency & Parallelism in Python
GIL - Concurrency & Parallelism in Python
 
Pyjion - a JIT extension system for CPython
Pyjion - a JIT extension system for CPythonPyjion - a JIT extension system for CPython
Pyjion - a JIT extension system for CPython
 
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMUSFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
 
EuroPython 2020 - Speak python with devices
EuroPython 2020 - Speak python with devicesEuroPython 2020 - Speak python with devices
EuroPython 2020 - Speak python with devices
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
 
Elasticwulf Pycon Talk
Elasticwulf Pycon TalkElasticwulf Pycon Talk
Elasticwulf Pycon Talk
 
Squeezing Blood From a Stone V1.2
Squeezing Blood From a Stone V1.2Squeezing Blood From a Stone V1.2
Squeezing Blood From a Stone V1.2
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Scaling Django with gevent
Scaling Django with geventScaling Django with gevent
Scaling Django with gevent
 
Not breaking userspace: the evolving Linux ABI
Not breaking userspace: the evolving Linux ABINot breaking userspace: the evolving Linux ABI
Not breaking userspace: the evolving Linux ABI
 
QEMU - Binary Translation
QEMU - Binary Translation QEMU - Binary Translation
QEMU - Binary Translation
 
Multicore
MulticoreMulticore
Multicore
 
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...
“TensorFlow Lite for Microcontrollers (TFLM): Recent Developments,” a Present...
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
淺談 Live patching technology
淺談 Live patching technology淺談 Live patching technology
淺談 Live patching technology
 
Multithreading by rj
Multithreading by rjMultithreading by rj
Multithreading by rj
 
Challenges in GPU compilers
Challenges in GPU compilersChallenges in GPU compilers
Challenges in GPU compilers
 
SMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiSMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgi
 
Kernel bug hunting
Kernel bug huntingKernel bug hunting
Kernel bug hunting
 
Kubernetes Me this Batman
Kubernetes Me this BatmanKubernetes Me this Batman
Kubernetes Me this Batman
 

Mais de Chetan Giridhar

Rapid development & integration of real time communication in websites
Rapid development & integration of real time communication in websitesRapid development & integration of real time communication in websites
Rapid development & integration of real time communication in websitesChetan Giridhar
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and pythonChetan Giridhar
 
PyCon India 2012: Rapid development of website search in python
PyCon India 2012: Rapid development of website search in pythonPyCon India 2012: Rapid development of website search in python
PyCon India 2012: Rapid development of website search in pythonChetan Giridhar
 
Fuse'ing python for rapid development of storage efficient FS
Fuse'ing python for rapid development of storage efficient FSFuse'ing python for rapid development of storage efficient FS
Fuse'ing python for rapid development of storage efficient FSChetan Giridhar
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python Chetan Giridhar
 
Testers in product development code review phase
Testers in product development   code review phaseTesters in product development   code review phase
Testers in product development code review phaseChetan Giridhar
 
Design patterns in python v0.1
Design patterns in python v0.1Design patterns in python v0.1
Design patterns in python v0.1Chetan Giridhar
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programmingChetan Giridhar
 

Mais de Chetan Giridhar (8)

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

Último

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Último (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Pycon11: Python threads: Dive into GIL!

  • 1. Python threads: Dive into GIL! PyCon India 2011 Pune, Sept 16-18 Vishal 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.7 Getting in to the Problem Space!! GIL
  • 5. Python Threads Real system threads (POSIX/ Windows threads) Python VM has no intelligence of thread management No thread priorities, pre-emption Native operative system supervises thread scheduling Python 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 interpreter Global interpreter lock (GIL) provides the synchronization (bookkeeping) GIL is necessary, mainly because CPython's memory management is not thread-safe.
  • 7. GIL: Code Details A thread create request in Python is just a pthread_create() call The function Py_Initialize() creates the GIL GIL is simply a synchronization primitive, and can be implemented with a semaphore/ mutex. ../Python/ceval.c static PyThread_type_lockinterpreter_lock = 0; /* This is the GIL */ A “runnable” thread acquires this lock and start execution
  • 8.
  • 9. A tick maps to a Python VM’s byte-code instructions
  • 10. Check interval can be set with sys.setcheckinterval (interval)
  • 11.
  • 14. Everyone competes for GILFile: ../ceval.c PyEval_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 machine GIL released waiting for GIL thread1 Running Suspended Signals thread2 GIL acquired waiting for GIL thread2 Suspended Wakeup Running time check1 check2
  • 16. GIL impact There is considerable time lag with Communication (Signaling) Thread wake-up GIL acquisition GIL Management: Independent of host/native OS scheduling Result Significant overhead Thread waits if GIL in unavailable Threads run sequentially, rather than concurrently Try Ctrl + C. Does it stop execution?
  • 17.
  • 18. Host OS can schedule threads concurrently on multi-core
  • 19.
  • 20. Understanding the new story!! Python v3.2 Getting in to the Problem Space!! GIL
  • 21. New GIL: Python v3.2 Regular “check” are discontinued We have new time-out mechanism. Default time-out= 5ms Configurable through sys.setswitchinterval() For every time-out, the current GIL holder is forced to release GIL It then signals the other waiting threads Waits 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.2 CPU Thread core0 time Waiting for the GIL GIL released Running Wait Suspended langis signal Time Out waiting for GIL GIL acquired Suspended Wakeup Running CPU Thread core1
  • 23. Positive impact with new GIL Better GIL arbitration Ensures that a thread runs only for 5ms Less context switching and fewer signals Multicore perspective: GIL battle eliminated! More responsive threads (fair scheduling) All iz well
  • 24. I/O threads in Python An interesting optimization by interpreter I/O calls are assumed blocking Python I/O extensively exercise this optimization with file, socket ops (e.g. read, write, send, recv calls) ./Python3.2.1/Include/ceval.h Py_BEGIN_ALLOW_THREADS Do some blocking I/O operation ... Py_END_ALLOW_THREADS I/O thread always releases the GIL
  • 25. Convoy effect: Fallout of I/O optimization When 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 threads GIL released GIL released I/O Thread core0 GIL acquired Time Out Running Wait Running Wait Suspended waiting for GIL GIL acquired Suspended Running Wait Suspended CPU Thread core1 time
  • 27. Performance measurements! Curious to know how convoy effect translates into performance numbers We performed following tests with Python3.2: An application with a CPU and a I/O thread Executed on a dual core machine CPU thread spends less than few seconds (<10s)!
  • 28. Comparing: Python 2.7 & Python 3.2 Performance dip still observed in dual cores 
  • 29. Getting into the problem space!! Python v2.7 / v3.2 Getting in to the Problem Space!! GIL Solution space!!
  • 30. GIL free world: Jython Jython is free of GIL  It can fully exploit multiple cores, as per our experiments Experiments with Jython2.5 Run with two CPU threads in tandem Experiment shows performance improvement on a multi-core system
  • 31. Avoiding GIL impact with multiprocessing multiprocessing — 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. Conclusion Multi-core systems are becoming ubiquitous Python applications should exploit this abundant power CPython inherently suffers the GIL limitation An intelligent awareness of Python interpreter behavior is helpful in developing multi-threaded applications Understand and use 
  • 33. Questions Thank you for your time and attention  Please share your feedback/ comments/ suggestions to us at: cjgiridhar@gmail.com , http://technobeans.com vishalkanaujia@gmail.com, http://freethreads.wordpress.com
  • 34. References Understanding the Python GIL, http://dabeaz.com/talks.html GlobalInterpreterLock, http://wiki.python.org/moin/GlobalInterpreterLock Thread State and the Global Interpreter Lock, http://docs.python.org/c-api/init.html#threads Python 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: GIL A thread needs GIL before updating Python objects, calling C/Python API functions Concurrency is emulated with regular ‘checks’ to switch threads Applicable to only CPU bound thread A blocking I/O operation implies relinquishing the GIL ./Python2.7.5/Include/ceval.h Py_BEGIN_ALLOW_THREADS Do some blocking I/O operation ... Py_END_ALLOW_THREADS Python file I/O extensively exercise this optimization
  • 37. GIL: Internals The function Py_Initialize() creates the GIL A thread create request in Python is just a pthread_create() call ../Python/ceval.c static 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.
  • 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.
  • 43. Convoy effect: Python v2? Convoy effect holds true for Python v2 also The 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 finish Should choose the setswitchinterval() wisely The effect is not so visible in Python v2.0
  • 44. Stackless Python A different way of creating threads: Microthreads! No improvement from multi-core perspective Round-robin scheduling for “tasklets” Sequential execution 

Notas do Editor

  1. Okokokokkokokokok