Debugging Tips and Tricks

0 0

my new master I want to thank the organizers and the psf for hosting pikin asia-pacific in Seoul this year my name is Roy Hyun Jin Han and I'm one of the main developers of cross compute which is a package on pi PI that you can pip install and it will convert your script until web at so today I'm going to be talking about Python debugging tips all of these slides are available on github at that link that's bit ly / pdb tips okay so we'll we're going to I'm going to discuss 11 I mean eight use cases for debugging and split into three categories when you're building your prototype when you're building the development version and the production version okay so when you're developing your prototype the first demo that i'm going to show is when you run an interpreter okay so who here is heard of Jupiter notebook yeah well jus Jupiter notebook has something interesting called debug so let's see a demo of this right now okay we're going to activate the virtual environment so and then install Jupiter and still I Python okay so now we're going to start the Jupiter notebook and I'm going to show you some debug feature which not many people know about okay so we're going to define a function that divides for X and we're going to make it raise an exception so this will raise an exception okay so we've got an exception but then we'll run debug and when we run the bug it drops us at that part into the ipd be debugger and then you can examine your variables and stuff like this okay so who knew about this the next so this is if you want when you're developing a prototype of how we recommend using Jupiter notebook the next thing that will show is ipython embed so this wherever you want to examine the variables in your program at that part just copy and paste this line assuming that you have I Python installed and you'll be able to see using the whose command so the whose command is very interesting in this demo so this is a simple script it adds two numbers it so takes to command line input arguments a and B converts and integers and adds them and we're going to insert ipython embed right inside the function okay and then when we hooks when we run the command and type who's who's will tell us which h 0 h wh 0 s would tell us what variables we can see in our scope so this is a command that not many people know about but has been very useful when i debug my programs okay so now on to the third scenario okay so in the third scenario we're going to use a package before we cover Jupiter covered ipython now we introduced something called PUD be so p 0 DB was made by the creator of pi cuda andreas klockner and he still maintains it very well everything that I'm introducing here is a very well maintained package and TOD be is very nice and if you remember back in the 80s there was a company called Borland and they had made something called borland c++ which also had Borland turbo debugger well pewdie b is a lot like turbo debugger so this is great because it revived brings back the 80s ok so we've installed pud be and this is the same script from before it adds two numbers and instead of ipython embed will set trace on t UD be so we just inserted that line now when we run it drops us into the pud be interface which is very nice on the upper right you have a watch list that you every time we step through by pressing n you can see what variables are visible and you can you know so this is you can set breakpoints as a amazing console console debugger ok so we talked about pud be yeah on the bottom you can do I Python stuff okay so in so i use PUD be a lot everything i'm showing is stuff that i use a lot once you've already developed your script then you have different options in case you don't want to modify the script you can just run this command pud b dash dash your script and whatever arguments that follow and the dash dash you only need it if you have command line arguments for your script and this is great because as we'll see running this command will drop you into the pu DVD bugger when immediately so see it starts at the beginning we didn't have to add any statements and we can type c to run the whole program or step through it within and just like before so this is another option you can use when you've already written your script and you just want to step through and understand how or remember how what you wrote okay so now this is another relatively new feature pud b which is when you press ctrl c it'll drop into the bugger so this is when you want to let's say you have some tornado event loop and you want to drop into the debugger so in this example we'll have a continuous while loop so you see it it's wild true it's kind of hard to see here but for me um yeah so when it runs this it it loops continuously and we're going to insert at the top something that listens for control C and this is just what you just saw was the Jedi plug-in for auto complete okay so now when you run this script so it it was running we press ctrl C and a dropped us right where it on when we press ctrl C so this is a different way to enter the debugger and don't worry well I'll have a summary table at the end ok so we covered just covered tracing when you press control-c however it's another matter entirely if you have a multi-threaded application because it's hard to launch a debugger and in the main process so there or if you have a docker container so because you can't directly asked to access it so there's another new package called wdb and it's pretty interesting so it has the same syntax you just drop this line and this is introduces remote debugging remote debugging has been around for a while and is it running yeah so let me just check that it's playing okay so i'm waiting for the let me restart the video okay so debugging has been around for a while oops okay okay so right now we're talking about ok so we skip to the multi-threaded container part so now this is interesting oh here we are ok so this one you just install I guess the video is not working here so I've just played this manually and ok so we're installing wdb so this is a simple threaded application so you'll see it'll start three workers and we're going to import wdb okay so now when we run this script oops yeah first we have to I don't know if you see at the top but first we have to start the WDB server and then it'll launch you see at the top it launches three threads a three individual well here let me let me show this one more time because this is a very interesting okay so so we'll try this one again so we're installing WTV and the reason i restarted this is because there's something important at the top you need to launch the background process the WDB server okay so this is you need to launch this and everything happens on your localhost and then when you do it let's see it it started three separate debugger sessions at the top one for each time you wash it inside your thread so you can see x over here oops my majors so you can see x over here was one two and three for each of the instances of the thread ok so let's go back into okay then once once your system is already in production it's very important to keep logs of everything and actually I recommend using rsyslog which is a standard package and foremost Linux systems and you should have a namespace blogger so let's just show that right now okay so here's an example of a so now we're just talking about the logging package which you should know at the top you have a package that we're calling XYZ and at the bottom call imports XYZ function and calls something in it so first when you run it it shows nothing and then just with the basic configuration it will show the different morning levels however once we since we set the logger to the debugging level then we can select what amount of information on a sea so this is if on a single server setup but when you want to have multiple processes and many servers then I recommend using rsyslog which you can set on a separate machine and send have all of your processes send logging messages to the rsyslog so this is good for micro services and the last important thing is so let's say you already have tests and you want to nope and your test fail well the only if you use PI tests the only package that's easily compatible with PI test is PDP p which is an improvement on PDB it adds some auto some other things because you can't use pdb directly so we just installed p DD PP and we're going to run these tests on this package which has some errors and we ran it with I test pdb and when there's an error it drops us into the prompt and then we can do our typical debugging stuff okay so to summarize here is the main summary and once again you can see all these up sides of the link and that's all thank you