GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines...

102
GIL

Transcript of GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines...

Page 1: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

GIL

Page 2: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

„CPython cannot into threads”

Page 3: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1992

Page 4: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

„CPython cannot into threads”❌

Page 5: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

„CPython cannot into threads”„CPython can run only on single core”

Page 6: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

PyPyJythonCPythonIronPythonBrython

Page 7: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

PyPyJythonCPythonIronPythonBrython

Page 8: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

PyPyJythonCPythonIronPythonBrython

Page 9: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Python

C

Page 10: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Python

C

Page 11: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

„CPython cannot into threads”„CPython can run only on single core”

Page 12: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

„CPython cannot into threads”„CPython can run only on single core”"CPython process can execute Python bytecode in one thread at the time"

Page 13: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

kolodziejj.info@unit03

Page 14: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 15: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Disclaimer

Page 16: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

So what’s the hassle?

Page 17: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Misconceptions

Page 18: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

CC-SA-BY, http://dibujando.net/dib/cthulhu-96836

Page 19: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Evolution

Page 20: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 21: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Concurrencyvs.

parallelism

Page 22: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 23: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 24: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 25: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 26: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 27: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

GILfinally

Page 28: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Protectorof

CPython realm

Page 29: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Reference counters

Page 30: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1. Tracing garbage collection2. Reference counting3. ...

Page 31: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1. Tracing garbage collection2. Reference counting

int

Page 32: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1. Tracing garbage collection2. Reference counting

3

Page 33: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1. Tracing garbage collection2. Reference counting

0

Page 34: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Internal global state

Page 35: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

C-mutable data structuresdict, list, …

Page 36: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

C-mutable data structuresdict, list, …str, tuple, int

Page 37: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Binary modulesand extensions

Page 38: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Cost?

Page 39: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 40: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

In single-threaded code?It doesn’t matter

(duh)

Page 41: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

On single core?It doesn’t matter

Page 42: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

In multi-threaded I/O-bound code?

It may not matter

Page 43: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

With binary extensions?It may not matter

Page 44: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

>= 3.2

Page 45: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1

Page 46: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1

2

Page 47: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1

2

Can Ihas GIL?

Nope.

Page 48: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1

2switch interval

Page 49: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

Can Ihas GIL?

Page 50: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

Can Ihas GIL?

Yes!

Page 51: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

Can Ihas GIL?

Nope.

Page 52: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

gil_drop_request = 1

Page 53: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

gil_drop_request?

Page 54: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

Page 55: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

switch_cond?gil_last_holder?

Page 56: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

Page 57: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

<= 2.7- 2010

Page 58: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

„ticks”

Page 59: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

>>> print(„foo”)>>> -1 in range(100000000)

Page 60: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

>>> print(„foo”)>>> -1 in range(100000000)>>> n -= 1

Page 61: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

„check”„check” „check”

N „ticks” N „ticks” N = 100

Page 62: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 63: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

100s ms few ms

Page 64: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

Can Ihas GIL?

Nope.

And now? And now?

Nope. Nope.

Page 65: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

2

1

Can Ihas GIL?

Nope.

And now? And now?

Nope. Nope.

Aw yiss

Page 66: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

PyPyJythonCPythonIronPythonBrython

Page 67: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

PyPyJythonCPythonIronPythonBrython

Page 68: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

PyPyJythonCPythonIronPythonBrython

(but: STM)

Page 69: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

PyPyJythonCPythonIronPythonBrython

Page 70: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

RubyPythonGo

Page 71: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

RubyPythonGo

Page 72: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

RubyPythonGoroutines

Page 73: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

RubyPythonGoroutineschannels

Page 74: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

More!

Page 75: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo
Page 76: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

multiprocessingimport

Page 77: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

multiprocessingbinary extensions

cdef int foo = 0

Page 78: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

multiprocessingbinary extensions

cdef int foo = 0with nogil: ...

Page 79: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

multiprocessingbinary extensions

Py_BEGIN_ALLOW_THREADS Py_END_ALLOW_THREADS

Page 80: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

multiprocessingbinary extensionsasynchronous I/O

Page 81: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Revolution?

Page 82: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

C

Page 83: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Requirements

Page 84: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1. Single-threaded and multi-threaded I/O-bound performance2. C extensions compatibility3. Complexity

Page 85: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1. Single-threaded and multi-threaded I/O-bound performance2. C extensions compatibility3. Complexity

Page 86: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1. Single-threaded and multi-threaded I/O-bound performance2. C extensions compatibility3. Complexity

Page 87: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

1996

Page 88: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Free-threading patch

Page 89: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Free-threading patchsubinterpreters

Page 90: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Free-threading patchsubinterpreters

GILectomy

Page 91: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Free-threading patchsubinterpreters

GILectomybuffered

reference counting

Page 92: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Free-threading patchsubinterpreters

GILectomy

thread

-privat

e

locking

buffered

reference counting

Page 93: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Free-threading patchsubinterpreters

GILectomy

thread

-privat

e

locking

buffered

reference counting

immortal objects

Page 94: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

--without-gilPy_LOCK(o)Py_UNLOCK(o)

Page 95: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

Free-threading patchsubinterpreters

GILectomy

Page 96: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

What to do?

Page 97: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

What to do?nothing

Page 98: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

What to do?nothing

more research

Page 99: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

What to do?nothing

more researchmigrate to 3.5 (3.6 even!)

Page 100: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

What to do?nothing

more researchmigrate to 3.5 (3.6 even!)

assert the baseline, then act

Page 101: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

kolodziejj.info/talks/gil/

Thank you! :)

Page 102: GIL - Unit Foto · „CPython cannot into threads” ... Goroutines. Ruby Python Goroutines channels. More! import multiprocessing. multiprocessing binary extensions cdef int foo

kolodziejj.info/talks/gil/

What to do?nothing

more researchmigrate to 3.5 (3.6 even!)

assert the baseline, then actcontribute !