Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

multithreading - Not working python breakpoints in C thread in pycharm or eclipse+pydev

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...