dieter at handshake
Jun 12, 2012, 10:58 PM
Post #2 of 2
Salman Malik <salmanmk [at] live> writes:
> I am sort of a newbie to Python ( have just started to use pdb).
> My problem is that I am debugging an application that uses greenlets and when
> I encounter something in code that spawns the coroutines or wait for an event,
> I lose control over the application (I mean that after that point I can no
> longer do 'n' or 's' on the code). Can anyone of you tell me how to tame
> greenlet with pdb, so that I can see step-by-step as to what event does a
> coroutine sees and how does it respond to it.
> Any help would be highly appreciated.
Debugging works via the installation of a "tracehook" function. If such a
function is installed in a thread, the Python interpreter calles back
via the installed hook to report events relevant for debugging.
Usually the hook function is defined by a debugger which examines
whether the event is user relevant (e.g. if a breakpoint has been hit,
or code for a new line has been entered) and in this case imforms
the user and may give him control.
It is important that the trace hook installation is thread specific. Otherwise,
debugging in a multithreaded environment would be a nightmare - as
events from multiple threads may arrive and seriously confuse
the debugger as well as the user.
I do not know "greenlet". However, I expect that it uses threads
under the hood to implement coroutines. In such a case, it would
be natural that debugging one coroutine would not follow the
execution into a different coroutine.
To change this, "greenlet" would need to specially support
the "tracehook" feature: when control is transfered to a different
coroutine, the "tracehook" would need to be transfered as well.
Personally, I am not sure that this would be a good idea
(I sometimes experience debugging interaction from different threads --
and I can tell you that it is a really nasty experience).
However, you can set the "tracehook" youself in your each of your
coroutines: "import pdb; pdb.set_trace()". This is called a "code breakpoint".
It installs the debuggers "tracehook" function in the current thread
and gives control to the debugger (i.e. it works like a breakpoint).
I use this quite frequently to debug multithreaded web applications
and it works quite well (sometimes with nasty experiences).
"pdb" is not optimal to for multithread debugging because it expects
to interact with a single thread only. For a good experience,
a thread aware extension would be necessary.