python / python/cpython

tkinter runs a busy-wait loop on interactive Python

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

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

extension-modules topic-tkinter type-bug
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ả

Bug report

Bug description:

In EventHook in Modules/_tkinter.c, we used to have the following block to run the event loop:

#if defined(WITH_THREAD) || defined(MS_WINDOWS)
        Py_BEGIN_ALLOW_THREADS
        if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
        tcl_tstate = event_tstate;
        
        result = Tcl_DoOneEvent(TCL_DONT_WAIT);
        
        tcl_tstate = NULL;
        if(tcl_lock)PyThread_release_lock(tcl_lock);
        if (result == 0)
            Sleep(Tkinter_busywaitinterval);
        Py_END_ALLOW_THREADS
#else
        result = Tcl_DoOneEvent(0);
#endif

in bpo-31370, support for threads-less builds was removed, and this block became

        Py_BEGIN_ALLOW_THREADS
        if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
        tcl_tstate = event_tstate;
        
        result = Tcl_DoOneEvent(TCL_DONT_WAIT);
        
        tcl_tstate = NULL;
        if(tcl_lock)PyThread_release_lock(tcl_lock);
        if (result == 0)
            Sleep(Tkinter_busywaitinterval);
        Py_END_ALLOW_THREADS

This is a bit unfortunate, as we now back to running a busy-wait loop. See Guido van Rossum's comment of May 23, 1998: 7bf15648

I guess this block could be

        if (self->threaded) {
            /* Allow other Python threads to run. */
            ENTER_TCL
            result = Tcl_DoOneEvent(0);
            LEAVE_TCL
        }
        else {
            Py_BEGIN_ALLOW_THREADS
            if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
            tcl_tstate = tstate;
            result = Tcl_DoOneEvent(TCL_DONT_WAIT);
            tcl_tstate = NULL;
            if(tcl_lock)PyThread_release_lock(tcl_lock);
            if (result == 0)
                Sleep(Tkinter_busywaitinterval);
            Py_END_ALLOW_THREADS
        }

in analogy with the event loop in _tkinter_tkapp_mainloop_impl. As self->threaded is true in most builds, this would avoid the busy-wait loop.

I am happy to submit a PR, but I'll wait until PR #140147 is in to avoid unnecessary complications to this code.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

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

Bắt đầu trong Modules/_tkinter.c tại EventHook và so sánh khối vòng lặp sự kiện của nó với _tkinter_tkapp_mainloop_impl. Kiểm tra PR #140147 trước khi thay đổi mã này. Hoàn thành nghĩa là Python tương tác tránh vòng lặp busy-wait trong các bản build có thread, đồng thời vẫn giữ nguyên hành vi không có thread được thể hiện.

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

Đánh giá

Công nghệ
c, python
Lĩnh vực
desktop
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
55/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.