在 pycharm 或 eclipse+pydev 的 C 线程中无法使用 python 断点 [英] Not working python breakpoints in C thread in pycharm or eclipse+pydev

查看:24
本文介绍了在 pycharm 或 eclipse+pydev 的 C 线程中无法使用 python 断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 C++ 库(通过 swig 导入)的 django 应用程序.C++ 库启动自己的线程,该线程在 Python 代码中调用回调.

I have a django application using a C++ library (imported via swig). The C++ library launches own thread which calls callbacks in Python code.

我无法在 python 代码中设置断点,无论是在 PyDev 还是 PyCharm 中.也尝试了gevent 兼容性"选项,但没有运气.

I cannot setup a breakpoint in python code, neither in PyDev nor PyCharm. Tried also 'gevent compatibility' option too with no luck.

我确认回调被正确调用,因为 logging.info 转储了预期的内容.在其他线程中设置的断点工作正常.因此,python 调试器似乎无法管理由非 python 代码中创建的线程调用的 python 代码中的断点.

I verified the callbacks are properly called as logging.info dumps what expected. Breakpoints set in other threads work fine. So it seems that python debuggers cannot manage breakpoints in python code called by threads created in non-python code.

有人知道解决方法吗?也许我可以使用一些神奇"的线程初始化序列?

Does anyone know a workaround? Maybe there is some 'magic' thread initialization sequence I could use?

推荐答案

您必须设置调试器机器才能使其在非 Python 线程上工作(这是在创建 Python 线程时自动完成的,但是当您创建一个Python 没有任何创建钩子的线程,你必须自己做)——请注意,对于某些框架——例如 QThread/Gevent——事情是猴子修补的,以便我们知道初始化并启动调试器,但对于其他框架,您必须自己做.

You have to setup the debugger machinery for it to work on non-python threads (this is done automatically when a Python thread is created, but when you create a thread for which Python doesn't have any creation hook, you have to do it yourself) -- note that for some frameworks -- such as QThread/Gevent -- things are monkey patched so that we know about the initialization and start the debugger, but for other frameworks you have to do it yourself.

为此,在启动线程后您必须调用:

To do that, after starting the thread you have to call:

import pydevd
pydevd.settrace(suspend=False, trace_only_current_thread=True)

请注意,如果您设置了 suspend=True,它会模拟手动断点并在代码的该点停止.

Note that if you had put suspend=True, it'd simulate a manual breakpoint and would stop at that point of the code.

这篇关于在 pycharm 或 eclipse+pydev 的 C 线程中无法使用 python 断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆