@@ -405,22 +405,37 @@ lists. When in doubt, use a mutex!
405
405
Can't we get rid of the Global Interpreter Lock?
406
406
------------------------------------------------
407
407
408
- .. XXX link to dbeazley's talk about GIL?
409
-
410
408
The :term:` global interpreter lock` (GIL) is often seen as a hindrance to Python's
411
409
deployment on high-end multiprocessor server machines, because a multi-threaded
412
410
Python program effectively only uses one CPU, due to the insistence that
413
411
(almost) all Python code can only run while the GIL is held.
414
412
415
- Back in the days of Python 1.5, Greg Stein actually implemented a comprehensive
413
+ With the approval of :pep:` 703` work is now underway to remove the GIL from the
414
+ CPython implementation of Python. Initially it will be implemented as an
415
+ optional compiler flag when building the interpreter, and so separate
416
+ builds will be available with and without the GIL. Long-term, the hope is
417
+ to settle on a single build, once the performance implications of removing the
418
+ GIL are fully understood. Python 3.13 is likely to be the first release
419
+ containing this work, although it may not be completely functional in this
420
+ release.
421
+
422
+ The current work to remove the GIL is based on a
423
+ ` fork of Python 3.9 with the GIL removed < https://github.com/colesbury/nogil> ` _
424
+ by Sam Gross.
425
+ Prior to that,
426
+ in the days of Python 1.5, Greg Stein actually implemented a comprehensive
416
427
patch set (the " free threading" patches) that removed the GIL and replaced it
417
- with fine-grained locking. Adam Olsen recently did a similar experiment
428
+ with fine-grained locking. Adam Olsen did a similar experiment
418
429
in his ` python-safethread < https://code.google.com/archive/p/python-safethread> ` _
419
- project. Unfortunately, both experiments exhibited a sharp drop in single-thread
430
+ project. Unfortunately, both of these earlier experiments exhibited a sharp
431
+ drop in single-thread
420
432
performance (at least 30% slower), due to the amount of fine-grained locking
421
- necessary to compensate for the removal of the GIL.
433
+ necessary to compensate for the removal of the GIL. The Python 3.9 fork
434
+ is the first attempt at removing the GIL with an acceptable performance
435
+ impact.
422
436
423
- This doesn't mean that you can't make good use of Python on multi-CPU machines!
437
+ The presence of the GIL in current Python releases
438
+ doesn't mean that you can't make good use of Python on multi-CPU machines!
424
439
You just have to be creative with dividing the work up between multiple
425
440
*processes* rather than multiple *threads*. The
426
441
:class:` ~concurrent.futures.ProcessPoolExecutor` class in the new
@@ -434,22 +449,13 @@ thread of execution is in the C code and allow other threads to get some work
434
449
done. Some standard library modules such as :mod:` zlib` and :mod:` hashlib`
435
450
already do this.
436
451
437
- It has been suggested that the GIL should be a per-interpreter-state lock rather
438
- than truly global; interpreters then wouldn't be able to share objects.
439
- Unfortunately, this isn't likely to happen either. It would be a tremendous
440
- amount of work, because many object implementations currently have global state.
441
- For example, small integers and short strings are cached; these caches would
442
- have to be moved to the interpreter state. Other object types have their own
443
- free list; these free lists would have to be moved to the interpreter state.
444
- And so on.
445
-
446
- And I doubt that it can even be done in finite time, because the same problem
447
- exists for 3rd party extensions. It is likely that 3rd party extensions are
448
- being written at a faster rate than you can convert them to store all their
449
- global state in the interpreter state.
450
-
451
- And finally, once you have multiple interpreters not sharing any state, what
452
- have you gained over running each interpreter in a separate process?
452
+ An alternative approach to reducing the impact of the GIL is
453
+ to make the GIL a per-interpreter-state lock rather than truly global.
454
+ This was :ref:` first implemented in Python 3.12 < whatsnew312-pep684> ` and is
455
+ available in the C API. A Python interface to it is expected in Python 3.13.
456
+ The main limitation to it at the moment is likely to be 3rd party extension
457
+ modules, since these must be written with multiple interpreters in mind in
458
+ order to be usable, so many older extension modules will not be usable.
453
459
454
460
455
461
Input and Output
0 commit comments