2 GIL的过去和未来 张佳圆

2020-03-01 460浏览

  • 1.PyCon China · 2019 The Past and Future of GIL Jiayuan Zhang 1
  • 2.Who Am I? 2
  • 3.Who Am I? Full-Stack Developer at IQIYI.Inc 2
  • 4.Who Am I? Full-Stack Developer at IQIYI.Inc Python, JavaScript, Lisp and (Rust) 2
  • 5.Who Am I? Full-Stack Developer at IQIYI.Inc Python, JavaScript, Lisp and (Rust) Open source contributor, werkzeug, requests, doom-emacs, etc. 2
  • 6.Who Am I? Full-Stack Developer at IQIYI.Inc Python, JavaScript, Lisp and (Rust) Open source contributor, werkzeug, requests, doom-emacs, etc. Coding with Emacs, organizing my life with org-mode 2
  • 7.Outline 3
  • 8.Outline What is GIL 3
  • 9.Outline What is GIL How GIL works 3
  • 10.Outline What is GIL How GIL works Remove GIL 3
  • 11.Outline What is GIL How GIL works Remove GIL The future 3
  • 12.Part I What is GIL? 4
  • 13.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # single_threaded.py import time from threading import Thread COUNT = 50000000 def countdown(n): while n > 0: n -= 1 start = time.time() countdown(COUNT) end = time.time() print('Time taken in seconds -', end - start) 5
  • 14.1 $ python single_threaded.py 2 Time taken in seconds - 6.20024037361145 6
  • 15.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # multi_threaded.py import time from threading import Thread COUNT = 50000000 def countdown(n): while n > 0: n -= 1 t1 = Thread(target=countdown, args=(COUNT//2,)) t2 = Thread(target=countdown, args=(COUNT//2,)) start = time.time() t1.start() t2.start() t1.join() t2.join() end = time.time() print('Time taken in seconds -', end - start) 7
  • 16.1 $ python multi_threaded.py 2 Time taken in seconds - 6.924342632293701 8
  • 17.1 $ python single_threaded.py 2 Time taken in seconds - 6.20024037361145 1 $ python multi_threaded.py 2 Time taken in seconds - 6.924342632293701 9
  • 18.What is GIL ? 10
  • 19.What is GIL ? Global Interpreter Lock 10
  • 20.What is GIL ? Global Interpreter Lock Mutex, pthread (Linux) or win thread (Windows), controlled by OS 10
  • 21.What is GIL ? Global Interpreter Lock Mutex, pthread (Linux) or win thread (Windows), controlled by OS Allows only one thread to execute Python code at any point in time 10
  • 22.What is GIL ? Global Interpreter Lock Mutex, pthread (Linux) or win thread (Windows), controlled by OS Allows only one thread to execute Python code at any point in time Bottleneck in CPU-bound and multi-threaded code 10
  • 23.What is GIL in Depth 11
  • 24.What is GIL in Depth Is Python thread safe? 11
  • 25.What is GIL in Depth Is Python thread safe? "Lock" on what? 11
  • 26.Is Python Thread Safe? 12
  • 27.Is Python Thread Safe? 1 2 3 4 5 6 7 8 9 10 11 L.append(x) L1.extend(L2) x = L[i] x = L.pop() L1[i:j]= L2 L.sort() x = y x.field = y D[x] = y D1.update(D2) D.keys() 12
  • 28.Is Python Thread Safe? 1 2 3 4 5 6 7 8 9 10 11 L.append(x) L1.extend(L2) x = L[i] x = L.pop() L1[i:j]= L2 L.sort() x = y x.field = y D[x] = y D1.update(D2) D.keys() 1 2 3 4 i = i+1 L.append(L[-1]) L[i] = L[j] D[x] = D[x] + 1 12
  • 29.Is Python Thread Safe? 1 2 3 4 5 6 7 8 9 10 11 L.append(x) L1.extend(L2) x = L[i] x = L.pop() L1[i:j]= L2 L.sort() x = y x.field = y D[x] = y D1.update(D2) D.keys() 1 2 3 4 i = i+1 L.append(L[-1]) L[i] = L[j] D[x] = D[x] + 1 Safe 12
  • 30.Is Python Thread Safe? 1 2 3 4 5 6 7 8 9 10 11 L.append(x) L1.extend(L2) x = L[i] x = L.pop() L1[i:j]= L2 L.sort() x = y x.field = y D[x] = y D1.update(D2) D.keys() Safe 1 2 3 4 i = i+1 L.append(L[-1]) L[i] = L[j] D[x] = D[x] + 1 Not Safe 12
  • 31.Is Python Thread Safe? 1 2 3 4 5 6 7 8 9 10 11 L.append(x) L1.extend(L2) x = L[i] x = L.pop() L1[i:j]= L2 L.sort() x = y x.field = y D[x] = y D1.update(D2) D.keys() Safe 1 2 3 4 i = i+1 L.append(L[-1]) L[i] = L[j] D[x] = D[x] + 1 Not Safe What kinds of global value mutation are thread-safe? 12
  • 32.Why? 13
  • 33.How Python Runs? 14
  • 34.How Python Runs? 14
  • 35.How Python Runs? Python Source Code (.py files) 14
  • 36.How Python Runs? Python Source Code (.py files) 14
  • 37.How Python Runs? Python Source Code (.py files) 14
  • 38.How Python Runs? Python Source Code (.py files) Python Interpreter 14
  • 39.How Python Runs? Python Source Code (.py files) Python Interpreter 14
  • 40.How Python Runs? Python Source Code (.py files) Python Interpreter 14
  • 41.How Python Runs? Python Source Code (.py files) Python Interpreter Result 14
  • 42.What is Python Interpreter? 15
  • 43.What is Python Interpreter? 15
  • 44.What is Python Interpreter? Python Source Code (.py files) 15
  • 45.What is Python Interpreter? Python Source Code (.py files) 15
  • 46.What is Python Interpreter? Python Source Code (.py files) 15
  • 47.What is Python Interpreter? Python Source Code (.py files) Python Interpreter 15
  • 48.What is Python Interpreter? Python Source Code (.py files) Python Interpreter 15
  • 49.What is Python Interpreter? Compiler Python Source Code (.py files) Python Interpreter 15
  • 50.What is Python Interpreter? Compiler Python Source Code (.py files) Python Interpreter 15
  • 51.What is Python Interpreter? Compiler Python Source Code (.py files) Python Interpreter 15
  • 52.What is Python Interpreter? Compiler Bytecode Python Source Code (.py files) Python Interpreter 15
  • 53.What is Python Interpreter? Compiler Bytecode Python Source Code (.py files) Python Interpreter 15
  • 54.What is Python Interpreter? Compiler Bytecode Python Source Code (.py files) Python Interpreter 15
  • 55.What is Python Interpreter? Compiler Bytecode Virtual Machine Python Source Code (.py files) Python Interpreter 15
  • 56.What is Python Interpreter? Compiler Bytecode Virtual Machine Python Source Code (.py files) Python Interpreter 15
  • 57.What is Python Interpreter? Compiler Bytecode Virtual Machine Python Source Code (.py files) Python Interpreter Library Modules 15
  • 58.What is Python Interpreter? Compiler Bytecode Virtual Machine Python Source Code (.py files) Python Interpreter Library Modules 15
  • 59.What is Python Interpreter? Compiler Bytecode Virtual Machine Python Source Code (.py files) Python Interpreter Library Modules 15
  • 60.What is Python Interpreter? Compiler Bytecode Virtual Machine Python Source Code (.py files) Python Interpreter Library Modules 15
  • 61.What is Python Interpreter? Compiler Bytecode Virtual Machine Python Source Code (.py files) Result Python Interpreter Library Modules 15
  • 62."Lock" on What? Compiler Bytecode Virtual Machine Python Source Code (.py files) Result Python Interpreter Library Modules 16
  • 63."Lock" on What? GIL Compiler Bytecode Virtual Machine Python Source Code (.py files) Result Python Interpreter Library Modules 16
  • 64."Lock" on What? GIL Compiler Bytecode Virtual Machine Python Source Code (.py files) Result Python Interpreter Library Modules 16
  • 65.Try Some Bytecode Is `number += 1` thread safe? 17
  • 66.Try Some Bytecode Is `number += 1` thread safe? 1 from dis import dis 2 3 dis(lambdax:x+1) 17
  • 67.Try Some Bytecode 1 2 3 4 1 0 2 4 6 LOAD_FAST LOAD_CONST BINARY_ADD RETURN_VALUE GIL GIL GIL GIL 0 (x) 1 (1) 18
  • 68.Try Some Bytecode 1 2 3 4 1 0 2 4 6 LOAD_FAST LOAD_CONST BINARY_ADD RETURN_VALUE GIL GIL GIL GIL 0 (x) 1 (1) Not Thread Safe!!! 18
  • 69.Try Some Bytecode 1 2 3 4 1 0 2 4 6 LOAD_FAST LOAD_CONST BINARY_ADD RETURN_VALUE GIL GIL GIL GIL 0 (x) 1 (1) Not Thread Safe!!! 18
  • 70.Part II How GIL works? 19
  • 71.I/O Bound Module David Beazley Understand GIL20
  • 72.I/O Bound Module Thread 1 OS Thread 2 David Beazley Understand GIL20
  • 73.I/O Bound Module Thread 1 OS Thread 2 David Beazley Understand GIL20
  • 74.I/O Bound Module I/O Thread 1 OS Thread 2 David Beazley Understand GIL20
  • 75.I/O Bound Module I/O Thread 1 OS Thread 2 Release GIL David Beazley Understand GIL20
  • 76.I/O Bound Module I/O Thread 1 OS Thread 2 Release GIL Acquire GIL David Beazley Understand GIL20
  • 77.I/O Bound Module I/O Thread 1 OS Thread 2 Release GIL Acquire GIL David Beazley Understand GIL20
  • 78.I/O Bound Module I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL David Beazley Understand GIL20
  • 79.I/O Bound Module I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL David Beazley Understand GIL20
  • 80.I/O Bound Module I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL20
  • 81.I/O Bound Module I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL20
  • 82.I/O Bound Module I/O I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL20
  • 83.I/O Bound Module I/O I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL Release GIL David Beazley Understand GIL20
  • 84.I/O Bound Module I/O I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL20
  • 85.I/O Bound Module I/O I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL20
  • 86.I/O Bound Module I/O I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL Release GIL Acquire GIL When a thread is running, it holds the GIL David Beazley Understand GIL20
  • 87.I/O Bound Module I/O I/O I/O Thread 1 OS Thread 2 Release GIL Acquire GIL Release GIL Acquire GIL Release GIL Acquire GIL When a thread is running, it holds the GIL GiL released on I/O (read, write, send, recv, etc.) David Beazley Understand GIL20
  • 88.CPU Bound Module David Beazley Understand GIL21
  • 89.CPU Bound Module Thread 1 OS Thread 2 David Beazley Understand GIL21
  • 90.CPU Bound Module Thread 1 OS Thread 2 David Beazley Understand GIL21
  • 91.CPU Bound Module Thread 1 run 100 ticks OS Thread 2 David Beazley Understand GIL21
  • 92.CPU Bound Module Check Thread 1 run 100 ticks OS Thread 2 David Beazley Understand GIL21
  • 93.CPU Bound Module Check Thread 1 run 100 ticks OS Thread 2 Release GIL David Beazley Understand GIL21
  • 94.CPU Bound Module Check Thread 1 run 100 ticks OS Thread 2 Release GIL Acquire GIL David Beazley Understand GIL21
  • 95.CPU Bound Module Check Thread 1 run 100 ticks OS Thread 2 Release GIL Acquire GIL David Beazley Understand GIL21
  • 96.CPU Bound Module Check Thread 1 run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL David Beazley Understand GIL21
  • 97.CPU Bound Module Check Thread 1 Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL David Beazley Understand GIL21
  • 98.CPU Bound Module Check Thread 1 Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL David Beazley Understand GIL21
  • 99.CPU Bound Module Check Thread 1 Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL21
  • 100.CPU Bound Module Check Thread 1 Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL21
  • 101.CPU Bound Module Check Thread 1 Check run 100 ticks run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL21
  • 102.CPU Bound Module Check Thread 1 Check run 100 ticks Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL21
  • 103.CPU Bound Module Check Thread 1 Check run 100 ticks Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Release GIL David Beazley Understand GIL21
  • 104.CPU Bound Module Check Thread 1 Check run 100 ticks Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL21
  • 105.CPU Bound Module Check Thread 1 Check run 100 ticks Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL21
  • 106.CPU Bound Module Check Thread 1 Check run 100 ticks Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL David Beazley Understand GIL21
  • 107.CPU Bound Module Check Thread 1 Check run 100 ticks Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL CPU bound threads that never perform I/O are handled as a special case David Beazley Understand GIL21
  • 108.CPU Bound Module Check Thread 1 Check run 100 ticks Check run 100 ticks OS Thread 2 run 100 ticks Release GIL Acquire GIL run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL CPU bound threads that never perform I/O are handled as a special case A "check" occurs every 100 "ticks" David Beazley Understand GIL21
  • 109.Some OS Stuff David Beazley Understand GIL22
  • 110.Some OS Stuff The operating system has a priority queue of threads/processes ready to run David Beazley Understand GIL22
  • 111.Some OS Stuff The operating system has a priority queue of threads/processes ready to run Signaled threads simply enter that queue David Beazley Understand GIL22
  • 112.Some OS Stuff The operating system has a priority queue of threads/processes ready to run Signaled threads simply enter that queue The operating system then runs the process or thread with the highest priority David Beazley Understand GIL22
  • 113.Some OS Stuff The operating system has a priority queue of threads/processes ready to run Signaled threads simply enter that queue The operating system then runs the process or thread with the highest priority It may or may not be the signaled thread David Beazley Understand GIL22
  • 114.Real CPU Bound Thread Situation David Beazley Understand GIL23
  • 115.Real CPU Bound Thread Situation Thread 1 CPU 1 OS Thread 2 CPU 1 David Beazley Understand GIL23
  • 116.Real CPU Bound Thread Situation Thread 1 CPU 1 OS Thread 2 CPU 1 David Beazley Understand GIL23
  • 117.Real CPU Bound Thread Situation Thread 1 CPU 1 run 100 ticks OS Thread 2 CPU 1 David Beazley Understand GIL23
  • 118.Real CPU Bound Thread Situation check Thread 1 CPU 1 run 100 ticks OS Thread 2 CPU 1 David Beazley Understand GIL23
  • 119.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 120.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 121.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 122.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 123.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL run 100 ticks Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 124.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 125.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL Release GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 126.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 127.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 128.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 129.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL run 100 ticks Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 130.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 131.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 132.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 133.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 134.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 1 David Beazley Understand GIL23
  • 135.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS Thread 2 CPU 1 run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL run 100 ticks David Beazley Understand GIL23
  • 136.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS Thread 2 CPU 1 run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Idle run 100 ticks David Beazley Understand GIL23
  • 137.Real CPU Bound Thread Situation check Thread 1 CPU 1 OS Thread 2 CPU 1 run 100 ticks Release GIL check run 100 ticks Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Idle run 100 ticks Hundreds to thousands of checks might occur before a thread context switch David Beazley Understand GIL23
  • 138.Thread Thrashing David Beazley Understand GIL24
  • 139.Thread Thrashing Thread 1 CPU 1 OS Thread 2 CPU 2 David Beazley Understand GIL24
  • 140.Thread Thrashing Thread 1 CPU 1 OS Thread 2 CPU 2 David Beazley Understand GIL24
  • 141.Thread Thrashing Thread 1 CPU 1 run 100 ticks OS Thread 2 CPU 2 David Beazley Understand GIL24
  • 142.Thread Thrashing check Thread 1 CPU 1 run 100 ticks OS Thread 2 CPU 2 Wake David Beazley Understand GIL24
  • 143.Thread Thrashing check Thread 1 CPU 1 OS run 100 ticks Release GIL Thread 2 CPU 2 Wake David Beazley Understand GIL24
  • 144.Thread Thrashing check Thread 1 CPU 1 OS run 100 ticks Release GIL Acquire GIL Thread 2 CPU 2 Wake David Beazley Understand GIL24
  • 145.Thread Thrashing check Thread 1 CPU 1 OS run 100 ticks Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) David Beazley Understand GIL24
  • 146.Thread Thrashing check Thread 1 CPU 1 OS run 100 ticks Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) David Beazley Understand GIL24
  • 147.Thread Thrashing check Thread 1 CPU 1 OS run 100 ticks run 100 ticks Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) David Beazley Understand GIL24
  • 148.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake David Beazley Understand GIL24
  • 149.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL Release GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake David Beazley Understand GIL24
  • 150.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake David Beazley Understand GIL24
  • 151.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) David Beazley Understand GIL24
  • 152.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) David Beazley Understand GIL24
  • 153.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL run 100 ticks Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) David Beazley Understand GIL24
  • 154.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL check run 100 ticks Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) Wake David Beazley Understand GIL24
  • 155.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) Wake David Beazley Understand GIL24
  • 156.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) Wake David Beazley Understand GIL24
  • 157.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) Wake Acquire GIL (success) David Beazley Understand GIL24
  • 158.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) Wake Acquire GIL (success) David Beazley Understand GIL24
  • 159.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 run 100 ticks CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) Wake Acquire GIL (success) David Beazley Understand GIL24
  • 160.Thread Thrashing check Thread 1 CPU 1 OS check run 100 ticks run 100 ticks Release GIL Acquire GIL check run 100 ticks Release GIL Acquire GIL Release GIL Acquire GIL Thread 2 run 100 ticks CPU 2 Wake Acquire GIL (fails) Wake Acquire GIL (fails) Wake Acquire GIL (success) When thread 2 wakes up, the GIL is already gone David Beazley Understand GIL24
  • 161.A Better GIL after Python 3.2 25
  • 162.A Better GIL after Python 3.2 It aims to fix thread thrashing 25
  • 163.A Better GIL after Python 3.2 It aims to fix thread thrashing Current thread will voluntarily release the GIL if it runs out of TIMEOUTs 25
  • 164.A Better GIL after Python 3.2 It aims to fix thread thrashing Current thread will voluntarily release the GIL if it runs out of TIMEOUTs If other thread acquire the GIL, the current thread will release the GIL after 5ms 25
  • 165.A Better GIL after Python 3.2 It aims to fix thread thrashing Current thread will voluntarily release the GIL if it runs out of TIMEOUTs If other thread acquire the GIL, the current thread will release the GIL after 5ms A thread runs until `gil_drop_request` gets set to 1 25
  • 166.But, the GIL is still there... 26
  • 167.Part III Remove GIL? 27
  • 168.Why GIL? 28
  • 169.Why GIL? There was no multi-core computer when Python was created 28
  • 170.Why GIL? There was no multi-core computer when Python was created Python is designed to be easy-to-use, so you don't need to care about the memory stuff 28
  • 171.Why GIL? There was no multi-core computer when Python was created Python is designed to be easy-to-use, so you don't need to care about the memory stuff GIL prevents deadlocks (as there is only one lock) 28
  • 172.Why GIL? There was no multi-core computer when Python was created Python is designed to be easy-to-use, so you don't need to care about the memory stuff GIL prevents deadlocks (as there is only one lock) GIL provides a performance increase to single-threaded programs as only one lock needs to be managed 28
  • 173.Why GIL? There was no multi-core computer when Python was created Python is designed to be easy-to-use, so you don't need to care about the memory stuff GIL prevents deadlocks (as there is only one lock) GIL provides a performance increase to single-threaded programs as only one lock needs to be managed CPython uses Reference Counting 28
  • 174.Before Removing the GIL... 29
  • 175.Before Removing the GIL... 1. Reference Counting 29
  • 176.Before Removing the GIL... 1. Reference Counting 2. Globals and statics in the interpreter 29
  • 177.Before Removing the GIL... 1. Reference Counting 2. Globals and statics in the interpreter 3. The C extension parallelism and reentrancy issues need to be handled as do places in the code where atomicity is required 29
  • 178.Before Removing the GIL... 1. Reference Counting 2. Globals and statics in the interpreter 3. The C extension parallelism and reentrancy issues need to be handled as do places in the code where atomicity is required 4. You can't breaking all of the C extensions 29
  • 179.Before Removing the GIL... 1. Reference Counting 2. Globals and statics in the interpreter 3. The C extension parallelism and reentrancy issues need to be handled as do places in the code where atomicity is required 4. You can't breaking all of the C extensions “ I'd welcome a set of patches into Py3k only if the performance for a single-threaded program (and for a multi-threaded but I/O-bound program) does not decrease. -- Guido van Rossum 29
  • 180.Related Works 30
  • 181.Related Works 1995, Greg Stein, a fork of Python 1.5 30
  • 182.Related Works 1995, Greg Stein, a fork of Python 1.5 Larry Hastings' Gilectomy (on hold) 30
  • 183.Related Works 1995, Greg Stein, a fork of Python 1.5 Larry Hastings' Gilectomy (on hold) Many other implementations... 30
  • 184.It's Hard!!! 31
  • 185.But, Who Cares? 32
  • 186.But, Who Cares? Users with threaded, CPU bound Python code 32
  • 187.But, Who Cares? Users with threaded, CPU bound Python code 32
  • 188.But, Who Cares? Users with threaded, CPU bound Python code Use C extensions! 32
  • 189.But, Who Cares? Users with threaded, CPU bound Python code Basically no one else Use C extensions! 32
  • 190.Other Solutions Multi-processing (not recommend) C extension modules Rewrite CPU bound code in C (you need to control memory by yourself) Release the GIL around that code Corountine 33
  • 191.Part IV The Future 34
  • 192.PEP 554 35
  • 193.PEP 554 Multiple Interpreters in the Stdlib 35
  • 194.PEP 554 Multiple Interpreters in the Stdlib By Eric Snow (to GIL or not toGIL:the Future of Multi-Core CPython) 35
  • 195.PEP 554 Multiple Interpreters in the Stdlib By Eric Snow (to GIL or not toGIL:the Future of Multi-Core CPython) Will release in CPython 3.9 (maybe) 35
  • 196.PEP 554 Multiple Interpreters in the Stdlib By Eric Snow (to GIL or not toGIL:the Future of Multi-Core CPython) Will release in CPython 3.9 (maybe) CPython has supported multiple interpreters in the same process since version 1.5 (1997) via the C-API 35
  • 197.PEP 554 Multiple Interpreters in the Stdlib By Eric Snow (to GIL or not toGIL:the Future of Multi-Core CPython) Will release in CPython 3.9 (maybe) CPython has supported multiple interpreters in the same process since version 1.5 (1997) via the C-API Introduce the stdlib interpreters modules, high-level interface to subinterpreters 35
  • 198.PEP 554 Multiple Interpreters in the Stdlib By Eric Snow (to GIL or not toGIL:the Future of Multi-Core CPython) Will release in CPython 3.9 (maybe) CPython has supported multiple interpreters in the same process since version 1.5 (1997) via the C-API Introduce the stdlib interpreters modules, high-level interface to subinterpreters Functionality for sharing data between interpreters (channel) 35
  • 199.Subinterpreters Model 36
  • 200.Subinterpreters Model Process 36
  • 201.Subinterpreters Model Process 36
  • 202.Subinterpreters Model Process GIL Interpreter 36
  • 203.Subinterpreters Model Process GIL Interpreter GIL Interpreter 36
  • 204.Subinterpreters Model Process GIL Interpreter GIL Interpreter 36
  • 205.Subinterpreters Model Process GIL Interpreter GIL Interpreter Thread 36
  • 206.Subinterpreters Model Process GIL Interpreter GIL Interpreter Thread 36
  • 207.Subinterpreters Model Process GIL Interpreter Thread GIL Interpreter Thread 36
  • 208.Subinterpreters Model Process GIL Interpreter Thread GIL Interpreter Thread 36
  • 209.Subinterpreters Model Process GIL Interpreter Thread Thread GIL Interpreter Thread 36
  • 210.Subinterpreters Model Process GIL Interpreter Thread Thread GIL Interpreter Thread Sub-interpreter can't access other sub-interpreters' variables 36
  • 211.How to Sharing Data? 37
  • 212.How to Sharing Data? A mechanism centers around "channels" 37
  • 213.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes 37
  • 214.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created 37
  • 215.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:37
  • 216.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:None 37
  • 217.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:None bytes 37
  • 218.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:None bytes str 37
  • 219.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:None bytes str int 37
  • 220.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:None PEP 3118 buffer objects bytes str int 37
  • 221.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:None PEP 3118 buffer objects bytes PEP 554 channels str int 37
  • 222.How to Sharing Data? A mechanism centers around "channels" Similiar to queues and pipes Objects are not shared between iterpreters since they are tied to the interpreter in which they were created Only the following types will be supported fosharing:None PEP 3118 buffer objects bytes PEP 554 channels str int Support for other basic types (e.g. bool, float) will be added later 37
  • 223.How to Sharing Data Process GIL Interpreter Thread Thread GIL Interpreter Thread 38
  • 224.How to Sharing Data Process GIL Interpreter Thread Thread GIL Interpreter Thread 38
  • 225.How to Sharing Data Process GIL Interpreter Thread GIL Channel Thread Interpreter Thread 38
  • 226.How to Sharing Data Process GIL Interpreter Thread GIL Channel Thread Interpreter Thread 38
  • 227.Advantages 39
  • 228.Advantages 39
  • 229.Advantages Isolation 39
  • 230.Advantages Isolation Each interpreter has its own copy of all modules, classes, functions, and variables 39
  • 231.Advantages Isolation Each interpreter has its own copy of all modules, classes, functions, and variables But process-global state remains shared (file descriptors, builtin types...) 39
  • 232.Advantages Isolation Each interpreter has its own copy of all modules, classes, functions, and variables But process-global state remains shared (file descriptors, builtin types...) Potentially performance 39
  • 233.Advantages Isolation Each interpreter has its own copy of all modules, classes, functions, and variables But process-global state remains shared (file descriptors, builtin types...) Potentially performance multiprocessing < subinterpreter < threads ? 39
  • 234.Advantages Isolation Each interpreter has its own copy of all modules, classes, functions, and variables But process-global state remains shared (file descriptors, builtin types...) Potentially performance multiprocessing < subinterpreter < threads ? Provide a direct route to an alternate concurrency mode 39
  • 235.In Progress 40
  • 236.In Progresshttps://github.com/ericsnowcurrently/multi-core-python40
  • 237.In Progresshttps://github.com/ericsnowcurrently/multi-core-pythonResolve bugs 40
  • 238.In Progresshttps://github.com/ericsnowcurrently/multi-core-pythonResolve bugs Deal with C globals 40
  • 239.In Progresshttps://github.com/ericsnowcurrently/multi-core-pythonResolve bugs Deal with C globals Move some runtime state into the interpreter state 40
  • 240.In Progresshttps://github.com/ericsnowcurrently/multi-core-pythonResolve bugs Deal with C globals Move some runtime state into the interpreter state Including the GIL 40
  • 241.That's All. Thanks!!! 41
  • 242.ContactsHomepage:http://jiayuanzhang.com/Blog:http://blog.jiayuanzhang.com/GitHub:https://github.com/forrestchangTwitter:https://twitter.com/tisogaWechat (请备注公司/学校 + 姓名) 42