Package: siginfo
siginfois a small Python module to conveniently get status or debug info from running Python processes.siginfolistens forSIGUSR1orSIGUSR2[1]signals and prints info about the current callstack, local variables etc.Use casesYou have long running Python processes and want to check in on progress, but don’t want to print the progress all the time.
This is especially useful for I/O heavy operations.Imagine you’re reading in a large file and work on it line by line. Instead of printing the line-number in regular intervals, you can simply send a SIGUSR command from another shell session and see the current callstack or start an interactive debugger.How toSimply add the following two lines:fromsiginfoimportSiginfoBasicSiginfoBasic()to your python script. Once your application is running, you can always check the current callstack from another shell session by sending a SIGUSR signal.kill-SIGUSR1${pid}It will now print the current callstack with basic information about all local variables to stdout (or a predefined file).========================================================================================================================LEVEL0METHODread_linesLINENUMBER:33------------------------------------------------------------------------------------------------------------------------LOCALSVARIABLE|TYPE|VALUEi|int|1fh|TextIOWrapper|<_io.TextIOWrappername='many_rows.txt'mode='r'encoding='UTF-8'>b|int|15a|int|12line|str|Row1------------------------------------------------------------------------------------------------------------------------SCOPE<codeobjectread_linesat0x108c30c90,file"long_script.py",line24>CALLER<codeobjectmain2at0x108c309c0,file"long_script.py",line21>================================================================================================================================================================================================================================================LEVEL1METHODmain2LINENUMBER:22------------------------------------------------------------------------------------------------------------------------LOCALSVARIABLE|TYPE|VALUE------------------------------------------------------------------------------------------------------------------------SCOPE<codeobjectmain2at0x108c309c0,file"long_script.py",line21>CALLER<codeobjectmainat0x108c30ed0,file"long_script.py",line18>========================================================================================================================...InstallationYou can install siginfo via pippipinstallsiginfoUsageIncludesiginfoin your python scriptimportsiginfoInitialize SignInfo Classsiginfo.SiginfoBasic()Basic overviewsiginfocontains the following classes:SiginfoBasicPrint info about the current stack (and caller stacks). Regular execution continues automatically.SigInfoPDBOpen thePDBdebugger. Pauses script execution until debugger is exited.SigInfoSinglePrint the value of a single variable of the current scope. Continues regular execution automatically.Initiating the classAll class allow the following arguments:infoListen forSIGNFO(Default:True) (only on Mac and BSD)usr1Listen forSIGUSR1(Default:True)usr2Listen forSIGUSR2(Default:False)outputWhere to write the output to (Default:sys.stdout). Can be anything that offers awritefunction.fromsiginfoimportSiginfoBasicSiginfoBasic(info=True,usr1=False)# listen only for SIGINFOSiginfoBasic(info=False,usr=True)# listen only for SIGUSR1SiginfoBasic(output=open('mylog.log','a'))# Write call stack output to a log filesigninfoclass instance attributesCOLUMNS: Maximum width of the Terminal (or max number of rows per line in an output file) (Default: current tty columns - 20; Fallback to 80 if determination isn’t possible)MAX_LEVELS: Number of stack frames to print (Default: 1 [only the current one])OUTPUT: Same asoutputargument to the constructor function. Defines where to write the output to (Default:sys.stdout)fromsiginfoimportSiginfoBasicinfo_handler=SiginfoBasic()info_handler.COLUMNS=200# Format output to match 200 columnsinfo_handler.MAX_LEVELS=4# Print the current frame + 3 parent framesinfo_handler.OUTPUT=open('mylog.log','a')# write the output to mylog.logAPI docsFor a more detailed API description, check outthe full documentationFootnotes[1]In addition, on MacOS or BSD system, theSIGINFOsignal can be used as well.