为什么断点在 PyDev 中的 Swing 事件调度线程上不起作用?

发布于 2024-12-02 08:31:17 字数 1345 浏览 1 评论 0原文

我正在使用 Jython、Swing 和 PyDev (Eclipse)。

在 EDT(又名 AWT 事件队列?)上运行的任何代码都不会命中断点。

这包括:

  • 从 Swing 事件(例如 JButton 单击)调用的函数
  • 通过装饰器通过 SwingUtilities.invokeLater() 运行的函数(请参阅最后一个示例 此处
  • 注册为 Java 包(套接字)的函数 。类),我正在使用

Swing 事件代码 。重现:

from javax.swing import JFrame, JButton

def TestFunc(event):
    #breakpoints in this function don't work
    print "Hey"

if __name__ == '__main__':
    mainWindow = JFrame('Test', 
                        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                        size = (1024, 600))
    mainWindow.add(JButton("Hey", actionPerformed = TestFunc))
    mainWindow.visible = True

invokeLater() 重现代码:

from java.lang import Runnable
from javax.swing import SwingUtilities
import threading

class foo(Runnable):
    def __init__(self, bar):
        self.bar = bar

    def run(self):
        #breakpoints in this function don't work
        print threading.currentThread()
        print self.bar

if __name__ == '__main__':
    myFoo = foo(5)
    SwingUtilities.invokeLater(myFoo)

I'm using Jython, Swing, and PyDev (Eclipse).

Breakpoints are not being hit on any code that runs on the EDT (aka AWT Event Queue?).

This includes:

  • Functions that are invoked from a Swing event (eg JButton click)
  • Functions that, via a decorator, are run through SwingUtilities.invokeLater() (See the last example here.
  • Functions that registered as hooks to a Java package (socket class), that I'm using.

Swing event code to reproduce:

from javax.swing import JFrame, JButton

def TestFunc(event):
    #breakpoints in this function don't work
    print "Hey"

if __name__ == '__main__':
    mainWindow = JFrame('Test', 
                        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                        size = (1024, 600))
    mainWindow.add(JButton("Hey", actionPerformed = TestFunc))
    mainWindow.visible = True

invokeLater() code to reproduce:

from java.lang import Runnable
from javax.swing import SwingUtilities
import threading

class foo(Runnable):
    def __init__(self, bar):
        self.bar = bar

    def run(self):
        #breakpoints in this function don't work
        print threading.currentThread()
        print self.bar

if __name__ == '__main__':
    myFoo = foo(5)
    SwingUtilities.invokeLater(myFoo)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-12-09 08:31:17

这实际上是一个 Jython 问题。

即:在下面的代码中,当调用 TestFunc 时,应该调用来自trace_dispatch 的打印,但事实并非如此。

因此,Jython 跟踪实现没有像在这种情况下那样调用跟踪函数。您可以通过调用 import pydevd;pydevd.settrace(suspend=False) 来“帮助”PyDev 调试器,以便调试器发现该帧(即:在 TestFunc 的开头添加该行代码) 。

请注意,如果您不传递 suspend=False,它将充当代码中的断点并在该行停止执行。

import sys
import threading
def trace_dispatch(frame, event, arg):
    print frame.f_code.co_filename, frame.f_code.co_name
sys.settrace(trace_dispatch)
threading.settrace(trace_dispatch)

from javax.swing import JFrame, JButton

def TestFunc(event):
    print "Hey"

if __name__ == '__main__':
    mainWindow = JFrame('Test', 
                        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                        size = (1024, 600))
    mainWindow.add(JButton("Hey", actionPerformed = TestFunc))
    mainWindow.visible = True

It's actually a Jython issue.

I.e.: in the code below, when TestFunc is called, the print from the trace_dispatch should be called, but it's not.

So, the Jython tracing implementation is not calling the tracing function as it should in that situation. You can 'help' the PyDev debugger by calling import pydevd;pydevd.settrace(suspend=False) so that the debugger discovers about that frame (i.e.: in the start of TestFunc add that line of code).

Note that if you don't pass the suspend=False, it'll act as a breakpoint in the code and will stop the execution at that line.

import sys
import threading
def trace_dispatch(frame, event, arg):
    print frame.f_code.co_filename, frame.f_code.co_name
sys.settrace(trace_dispatch)
threading.settrace(trace_dispatch)

from javax.swing import JFrame, JButton

def TestFunc(event):
    print "Hey"

if __name__ == '__main__':
    mainWindow = JFrame('Test', 
                        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                        size = (1024, 600))
    mainWindow.add(JButton("Hey", actionPerformed = TestFunc))
    mainWindow.visible = True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文