Home  >  Q&A  >  body text

python中有没有像java中的jstack命令

天蓬老师天蓬老师2743 days ago985

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-17 13:06:00

    There is nothing as convenient and direct to use as jstack. Generally speaking, there are several solutions:

    • IDE: Use tools such as PyCharm, Eclpise with Pydev to debug, pause and check each call stack when you feel it hangs.
    • GDB: The advantage is that you can debug the native extension. The disadvantage is also obvious. You need to add Debug Symbols when compiling Python. This is not available by default. To put it simply, it is powerful but not worth the trouble.
    • pdb: Suitable for lower breakpoints...
    • pudb: Change the code. At the program entry import pudb; pudb.set_interrupt_handler(), you can press Ctrl-C to enter the interactive debugging environment when running. The ease of use is not bad.
    • Use a singal handler to print all stack information. Refer to the install_cry_handler implemented by celery, or look at signalhandler, which comes with Python 3.3.

    NOTE: One disadvantage of all solutions that rely on signal is that many POSIX functions will return EINTR when these interrupts are generated, directly causing Python exceptions, such as send, recv, read, write etc. . .

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:06:00

    I don’t know Java, so I don’t know what you want. Do you want to print out the call stacks of all threads? It doesn't seem to be easy.

    I am usually:

    1. Try to avoid using threads
    2. Print log. This can roughly infer where the thread is stuck
    3. Strace on each thread to see what system call they are stuck on

    reply
    0
  • PHPz

    PHPz2017-04-17 13:06:00

    It seems that it is not possible to use multi-threading directly in python because there is something called GIL. As for if you want to see the thread situation, you can use pdb to debug. Try it

    reply
    0
  • Cancelreply