Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

True thread parallelism with Python 3.12#442

Discussion options

For decades python developers have been trying to work around the infamousGlobal Interpreter Lock (aka GIL), which has been preventing true multi-threading. With modern CPUs having many CPU cores it is a terrible waste for python to be able to use only one at a time.

Well, the time has come and Python 3.12 brings a breakthrough in this area. Subinterpreters was an obscure and rarely used feature of python, that allowed threads to run in clean slate environment. But they still had to acquire the GIL before they could run code. This is no longer with Python 3.12.Each subinterpreter may have its own GIL, which means they can run in parallel. Truly!

This feature is not available in python code yet, but can be exploited using the C-API. It has also a number of limitations the most important of which are:

  • You cannot share python objects between the subinterpreters. Also, there are no standard primitives for synchronization or communication between the interpreters. But this will change in future versions.
  • Certain extension modules that have not been updated to use multi-phase inititalization (seePEP 489) may not be available.

But despite these limitations, the performance boost that comes from the exploitation of multiple cores makes this feature very desirable.

P4D has already added support for the new subinterpreters and has integrated that functionality within the TPythonThread. A new thread execution modeemNewInterpreterOwnGIL allows you to run python threads in parallel. There is a new demo (Demo 36) that shows off this new feature.

Here are the results of this demo on my machine demonstrating the performance boost you expect to get:

Classic Subinterpreter:prime count 78498prime count 78498prime count 78498prime count 78498prime count 78498Elapsed ms: 13695Subinterpreter with own GIL:prime count 78498prime count 78498prime count 78498prime count 78498prime count 78498Elapsed ms: 3482
You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
1 participant
@pyscripter

[8]ページ先頭

©2009-2025 Movatter.jp