python / python/cpython

Improve Py_mod_multiple_interpreters and Py_mod_gil Usability

Đang mở
#132,861 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.15 interpreter-core topic-C-API topic-free-threading topic-subinterpreters type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Feature or enhancement

Proposal:

Here are some deficiencies:

  • the slot name "Py_mod_multiple_interpreters" is a bit long
  • the "Py_MOD_MULTIPLE_INTERPRETERS_*" slot value names are a bit long
  • the slot name "Py_mod_gil" is overly coupled to the GIL (it's really about thread safety)
  • the difference between Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED and Py_MOD_PER_INTERPRETER_GIL_SUPPORTED is unclear
  • Py_MOD_PER_INTERPRETER_GIL_SUPPORTED applies to the vast majority of extensions and should be the default
  • Py_mod_gil doesn't have an option that explicitly covers the case where an external dependency is not thread-safe
  • currently Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED implies an external dependency is not thread-safe and/or isolated to each interpreter; it should be the other way around (and be a synonym for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED), which would introduce a need for a Py_MOD_PER_INTERPRETER_GIL_NOT_SUPPORTED

We could take a simple approach, working with the existing slots:

  • (maybe) shorten the names
  • add Py_MOD_PER_INTERPRETER_GIL_NOT_SUPPORTED
  • add something like Py_MOD_GIL_USED_FOR_EXTERNAL

I think we could do better, by replacing the existing slots with new ones that focus explicitly on what we care about:

  • new module def slot: Py_mod_isolation
    • Py_MOD_NOT_ISOLATED - the module has process-global state and/or objects (e.g. static types)
    • Py_MOD_ISOLATED (default) - the module's state/objects have been isolated to the module (i.e interpreter)
  • new module def slot: Py_mod_threadsafe
    • Py_MOD_NOT_THREADSAFE (default) - the module's own state/code is not thread-safe
    • Py_MOD_THREADSAFE - the module's state/code are thread-safe; external dependencies are covered by Py_mod_external_libs
  • new module def slot: Py_mod_external_libs
    • Py_MOD_EXTERN_NOT_THREADSAFE - at least one external dependency is not thread-safe (may be used without the GIL held)
    • Py_MOD_EXTERN_NOT_ISOLATED - at least one external dependency is not isolated to the module object (i.e. interpreter)
    • Py_MOD_EXTERN_ISOLATED (default) - all external dependencies are isolated and using them is thread-safe
  • for Py_mod_isolation and Py_mod_threadsafe, external dependencies are covered by Py_mod_external_libs
  • deprecate Py_mod_multiple_interpreters slot
  • deprecate Py_mod_gil slot

Regarding the defaults, for Py_mod_isolation, multi-phase init implies isolation (per PEP 489). For Py_mod_external_libs, we assume that the vast majority of modules will not have problematic dependencies.

Equivalents to current usage:

Py_mod_threadsafe Py_mod_external_libs Py_mod_gil
Py_MOD_NOT_THREADSAFE * Py_MOD_GIL_USED
Py_MOD_THREADSAFE Py_MOD_EXTERNAL_NOT_THREADSAFE Py_MOD_GIL_USED
Py_MOD_THREADSAFE Py_MOD_EXTERNAL_NOT_ISOLATED Py_MOD_GIL_NOT_USED
Py_MOD_THREADSAFE Py_MOD_EXTERNAL_ISOLATED Py_MOD_GIL_NOT_USED
Py_mod_isolation Py_mod_external_libs Py_mod_multiple_interpreters
Py_MOD_NOT_ISOLATED * Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED
Py_MOD_ISOLATED Py_MOD_EXTERNAL_NOT_THREADSAFE Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED
Py_MOD_ISOLATED Py_MOD_EXTERNAL_NOT_ISOLATED Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED
Py_MOD_ISOLATED Py_MOD_EXTERNAL_ISOLATED Py_MOD_PER_INTERPRETER_GIL_SUPPORTED

CC @encukou

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue không nêu tệp nguồn hoặc các bài kiểm thử nào; hãy bắt đầu bằng cách tìm các định nghĩa slot hiện có của Py_mod_multiple_interpreters và Py_mod_gil và xem xét hành vi hiện tại của chúng. Trước tiên, hãy thống nhất với các maintainer về tên slot được đề xuất, các giá trị mặc định và deprecations; yêu cầu hoàn thành là phải có thiết kế đã được thống nhất cùng với phần triển khai và các bài kiểm thử tương ứng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.