Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to debug python code under Linux

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to debug python code under Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Log of specific methods for debugging pytho code under Linux

Yes, indeed, it has to be emphasized how important adequate logging is to the application. You should record important things, and if your record is good enough, you can find problems in the log and save a lot of time.

If you've ever used print statements to debug your code, stop now, use logging.debug instead, start slowly, and disable it completely later.

Tracking is sometimes helpful to see how the program is executed. You can use IDE's debug conjugate ngn to run the program step by step, but you need to know what you are looking for, otherwise it will be a long process. There is a trace module in the standard library that can print all the content in the execution process (such as making coverage reports).

Python-mtrace-- trace script.py this will produce a lot of output (each line will be printed out, so you'd better go through the pipeline and use grep to look at only the parts you are interested in), for example:

Python-mtrace-- trace script.py | ``egrep`` ^ (mod1.py | mod2.py)'``- if you like the new feature, you can try smiley-it can display changes in the contents of variables and can be used to track programs remotely.

PDB

Import pdb pdb.set_trace () # opens up pdb prompt or:

Try: code that fails except: import pdb pdb.pm () # or pdb.post_mortem () or (press keyboard C to start the script):

Python-mpdb script.py as in REPL:

C or continue

Q or quit

L or list, which displays the source code in the current interface

W or where, showing backtracking

D or down, which shows the next interface for backtracking

U or up, which shows the previous interface of backtracking

Repeat the last command

Anything else, evaluate the source code in the current interface (t has some other commands)

Corcontinue

Qorquit

Lorlist, the source displayed in the current frame

Worwhere, showing backtracking

Dordown, backtrack for 1 frame downhill

Uorup, rise 1 frame backtrack

Enter and repeat the last command.

Almost anything, evaluate the Python code for the current frame (and a few other commands)

Can replace pdb's:

Ipdb (easy_install ipdb)-like ipython (automatic patching, color, etc.)

Pudb (easy_install pudb)-based on curses (gui-like), browsing the source code has a good performance.

Remote PDB sudo``apt-get ``install`` winpdb replaces pdb.set_trace ():

Import rpdb2 rpdb2.start_embedded_debugger ("secretpassword") now runs Winpdb, entering the password to File > Attach. Don't like Winpdb? Just run PDB through TCP

Use the following code:

Import loggging class Rdb (pdb.Pdb): "" This will run pdb as an ephemeral telnet service. Once you connect no one else can connect. On construction this object will block execution till a client has connected. Based on https://github.com/tamentis/rpdb I think... To use this:: Rdb (4444) .set_trace () Then run: telnet 127.0.0.1 4444 "" def _ init__ (self, port=0): self.old_stdout = sys.stdout self.old_stdin = sys.stdin self.listen_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) self.listen_socket.bind (('0.0.0.0') Port) if not port: logging.critical ("PDB remote session open on:% s", self.listen_socket.getsockname ()) print > > sys.__stderr__, "PDB remote session open on:", self.listen_socket.getsockname () sys.stderr.flush () self.listen_socket.listen (1) self.connected_socket Address = self.listen_socket.accept () self.handle = self.connected_socket.makefile ('rw') pdb.Pdb.__init__ (self, completekey='tab', stdin=self.handle, stdout=self.handle) sys.stdout = sys.stdin = self.handle def do_continue (self Arg): sys.stdout = self.old_stdout sys.stdin = self.old_stdin self.handle.close () self.connected_socket.close () self.listen_socket.close () self.set_continue () return 1 do_c = do_cont = do_continue def set_trace (): "" Opens a remote PDB on first available port. " Rdb = Rdb () rdb.set_trace () displays details

Want a REPL? What about IPython?

If you don't need an overall debugger, just start IPython with the following code:

Import IPython IPython.embed () standard Linux tool

I was surprised that they were not fully utilized. Through these toolsets you can figure out many problems such as these: from performance problems (too many system calls, memory allocation, etc.) to deadlocks, networks, disks, and so on.

The most useful thing about sudo apt-get install htop sudo htop is to reduce the weight to run strace, simply by running the quick-frozen strace- P 12345 or the strace-f command parameter (- f represents the strace branch process). There is usually a lot of output, and you'd better redirect the output to a file (add & > file name after the command) for more in-depth analysis.

Then there is ltrace, which is similar to strace but called through the library, with basically the same parameters. Lsof can provide the processing number of the ltrace/ strace you have seen. Use lsof-P 12345.

Let the tracking go deeper.

It's easy to use and can do a lot of things, as long as everyone has htop installed!

Now, to find the process you want, just press:

S display system call tracking (strace)

L display Library call tracking (ltrace)

L display lsof

Monitor

There is no better alternative, the server is constantly monitoring, have you ever found yourself using weird tracking methods to find out why and how resources are being consumed, then don't be bothered by iotop, iftop, htop, iostat, vmstat, etc., and use dstat, which can do what most of the above-mentioned tools can do and can do better!

It will continuously display your data in compact, stylish code coloring (pro, unlike iostat, vmstat yo), and you can always see the previous data (unlike iftop, iotop, htop).

Just run this:

Dstat-- cpu-- io--mem-- net-- load-- fs-- vm-- disk-util-- disk-tps-- freespace-- swap-``top``-io--``top``-bio-adv there are simpler ways to write, such as shell history (shell history) or rename command (aliases).

GDB

This is a rather complex and powerful tool, but I only cover the basics (settings and basic commands).

Sudo apt-get install gdb python-dbg zcat / usr/share/doc/python2.7/gdbinit.gz > ~ / .gdbinit run app with python2.7-dbg sudo gdb-p 12345 now use:

Bt- stack track (level C) pystack- python stack track, provided you have ~ / .gdbinit and use python-dbg c (continue)

Is there a segfaults? Use faulthandler!

All but Python 3.3will make this terrible error. Go back to Python 2.x.

As long as you do the following, you will find at least one cause of the paragraph error.

> ``import``faulthandler`` > faulthandler.enable () memory leak

OK, there are many tools here, some of which are specific to WSGI applications, like Dozer, but my favorite is definitely objgraph. It is so amazingly convenient and easy to use. It doesn't inherit from WSGI or anything else, so you need to find your own way to run the following code:

> import objgraph > objs = objgraph.by_type ("Request") [: 15] > > objgraph.show_backrefs (objs, max_depth=20, highlight=lambda v: v in objs, filename= "/ tmp/graph.png") Graph written to / tmp/objgraph-zbdM4z.dot (107 nodes) Image generated as / tmp/graph.png you will get a chart like this (warning: this chart is very large). You will also get dot output. Memory utilization

Sometimes you want to use less memory. Allocating less memory usually makes programs run faster and better, and users like to keep improving:)

There are many tools to use [1], but in my opinion the best is pytracemalloc-compared to other tools, its overhead is very small (it does not need to rely on speed-weakening sys.settrace) and its output is very detailed. The headache is its configuration, because you need to recompile python, but spt makes it easy to do.

Just run the following command, and then you can buy lunch or something else:

Apt-get source python2.7 cd python2.7-* wget https://github.com/wyplay/pytracemalloc/raw/master/python2.7_track_free_list.patch patch-p1 then install pytracemalloc (please note: if you are doing this in a virtual environment, you need to rebuild it after python reinstallation-just run virtualenv myenv): pip ``install`` pytracemalloc now You can encapsulate your application with the following code: import tracemalloc, time tracemalloc.enable () top = tracemalloc.DisplayTop (5000, # log the top 5000 locations file=open ('/ tmp/memory-profile-%s'% time.time ()) "w") top.show_lineno = True try: # code that needs to be traced finally: top.display () will get output like this: 2013-05-31 18:05:07: Top 5000 allocations per file and line # 1:... / site-packages/billiard/_connection.py:198: size=1288 KiB, count=70 (+ 0), average=18 KiB # 2:... / site-packages/billiard/_connection.py:199: size=1288 KiB, count=70 (+ 0) Average=18 KiB # 3:... / python2.7/importlib/__init__.py:37: size=459 KiB, count=5958 (+ 0), average=78 B # 4:... / site-packages/amqp/transport.py:232: size=217 KiB, count=6960 (+ 0), average=32 B # 5:... / site-packages/amqp/transport.py:231: size=206 KiB, count=8798 (+ 0), average=24 B # 6:... / site-packages/amqp/serialization.py:210: size=199 KiB, count=822 (+ 0) Average=248 B # 7:... / lib/python2.7/socket.py:224: size=179 KiB, count=5947 (+ 0), average=30 B # 8:... / celery/utils/term.py:89: size=172 KiB, count=1953 (+ 0), average=90 B # 9:... / site-packages/kombu/connection.py:281: size=153 KiB, count=2400 (+ 0), average=65 B # 10:... / site-packages/amqp/serialization.py:462: size=147 KiB, count=4704 (+ 0) This is the end of the average=32 B display details "how to debug python code under Linux". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report