我有一个间歇性挂起的 mpi4py
程序。我如何跟踪各个进程在做什么?
我可以在不同的终端运行程序,例如使用pdb
mpiexec -n 4 xterm -e "python -m pdb my_program.py"
但如果问题仅在大量进程(在我的情况下为 80 个)时出现,这会变得很麻烦。此外,使用 pdb
很容易捕获异常,但我需要查看跟踪以找出发生挂起的位置。
最佳答案
Python trace模块允许您跟踪程序执行。为了单独存储每个进程的痕迹,您需要将代码包装在一个函数中:
def my_program(*args, **kwargs):
# insert your code here
pass
然后用trace.Trace.runfunc
运行它:
import sys
import trace
# define Trace object: trace line numbers at runtime, exclude some modules
tracer = trace.Trace(
ignoredirs=[sys.prefix, sys.exec_prefix],
ignoremods=[
'inspect', 'contextlib', '_bootstrap',
'_weakrefset', 'abc', 'posixpath', 'genericpath', 'textwrap'
],
trace=1,
count=0)
# by default trace goes to stdout
# redirect to a different file for each processes
sys.stdout = open('trace_{:04d}.txt'.format(COMM_WORLD.rank), 'w')
tracer.runfunc(my_program)
现在每个进程的trace会被写入一个单独的文件trace_0001.txt
等。使用ignoredirs
和ignoremods
参数来省略low水平调用。
关于python - 调试并行 Python 程序 (mpi4py),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46856327/