为什么 Pycharm 调试器不在主线程外的断点处停止?
Why is Pycharm debugger not stopping at a breakpoint outside the main thread?
我正在使用 PyCharm 2016.2.3(内部版本#PY-162.1967.10)。我无法让调试器在主线程外的任何断点处停止,无论断点的挂起 属性 是 All 还是 Thread;或者如果那是整个程序中唯一的断点。线程是通过将 QObject 移动到 QThread 中来实现的。这是显示问题的简单代码。辅助线程中任意位置的断点(在 Master.do() 或 Worker.run() 内)将不会命中。
import sys
from PyQt5.QtCore import QCoreApplication, QObject, QThread, pyqtSignal, pyqtSlot
class Worker(QObject):
"""
Generic worker.
"""
start = pyqtSignal(str)
finished = pyqtSignal()
def __init__(self, function):
QObject.__init__(self)
self._function = function
self.start.connect(self.run)
@pyqtSlot()
def run(self):
#TODO Breakpoints inside this method will not be hit.
self._function()
self.finished.emit()
class Master(QObject):
"""
An object that will use the worker class.
"""
def __init__(self):
QObject.__init__(self)
def do(self):
#TODO Breakpoints inside this method will not be hit.
print("All done.")
QCoreApplication.quit()
def main():
app = QCoreApplication(sys.argv)
master = Master()
worker = Worker(master.do)
thread = QThread()
worker.moveToThread(thread)
thread.started.connect(worker.run)
# Terminating thread gracefully, or so.
worker.finished.connect(thread.quit)
worker.finished.connect(worker.deleteLater)
thread.finished.connect(thread.deleteLater)
thread.start()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
在这里找到答案:
基本上,我必须在启动后立即导入 pydevd 并在线程中添加以下内容:
pydevd.settrace(暂停=假,trace_only_current_thread=真)
我正在使用 PyCharm 2016.2.3(内部版本#PY-162.1967.10)。我无法让调试器在主线程外的任何断点处停止,无论断点的挂起 属性 是 All 还是 Thread;或者如果那是整个程序中唯一的断点。线程是通过将 QObject 移动到 QThread 中来实现的。这是显示问题的简单代码。辅助线程中任意位置的断点(在 Master.do() 或 Worker.run() 内)将不会命中。
import sys
from PyQt5.QtCore import QCoreApplication, QObject, QThread, pyqtSignal, pyqtSlot
class Worker(QObject):
"""
Generic worker.
"""
start = pyqtSignal(str)
finished = pyqtSignal()
def __init__(self, function):
QObject.__init__(self)
self._function = function
self.start.connect(self.run)
@pyqtSlot()
def run(self):
#TODO Breakpoints inside this method will not be hit.
self._function()
self.finished.emit()
class Master(QObject):
"""
An object that will use the worker class.
"""
def __init__(self):
QObject.__init__(self)
def do(self):
#TODO Breakpoints inside this method will not be hit.
print("All done.")
QCoreApplication.quit()
def main():
app = QCoreApplication(sys.argv)
master = Master()
worker = Worker(master.do)
thread = QThread()
worker.moveToThread(thread)
thread.started.connect(worker.run)
# Terminating thread gracefully, or so.
worker.finished.connect(thread.quit)
worker.finished.connect(worker.deleteLater)
thread.finished.connect(thread.deleteLater)
thread.start()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
在这里找到答案:
基本上,我必须在启动后立即导入 pydevd 并在线程中添加以下内容: pydevd.settrace(暂停=假,trace_only_current_thread=真)