Package: faultguard
FaultguardPreventing data loss of your Python3 application. Keeps important data both duplicated in memory and on disk.OverviewUsually, after a crash through e.g. a segmentation fault or a power outage, data of running applications is lost. In environments where this is problematic – e.g. when users should not loose their work or important data is collected –,faultguardprevents data loss in two ways:faultguardkeeps the selected data automatically backed up in a second process. This way, if your Python application crashes - even with a segmentation fault caused e.g. by an external library -, in most cases the backup process is still running and immediately provides its data to a rescue handling function that you can define. This even allows you to e.g. provide users with a custom graphical dialog informing about the crash and providing options for the recovered data.faultguardcan save the selected data automatically in customizable time intervals to a file from which it can be recovered on the next application launch.ExampleAn example using all features offaultguardcan be found inexample.py.UsageTo secure an application data usingfaultguard, you define alaunchfunction that receives a custom data dictionary fromfaultguard. This dictionary, although working like a usual dictionary and accepting all content that can be serialized usingpickle, is automatically backed up as described above. If the guarded application crashes, the backup process launches a crash handler in form of arescuefunction also defined by you and provides it with the backed up dictionary. Additionally, if you providefaultguardwith a time interval and a path for autosaves, it stores the data on disk and you can call therecovermethod to recover the file content and call yourrescuefunction.faultguardwill raise aRuntimeErrorwhen trying to write to an existing autosave file or reading the autosave file of a running process.Thefaultguardinterface is very simple - you just provide it with alaunchand arescuefunction and everything else works automatically. If you use autosaving, on application launch you should additionally check for backup files and useis_activeto see if the process corresponding to an autosave file is still active. If not, that would show thatfaultguarddid previously not exit properly, so you can then letfaultguardrecoverthe file.Technical descriptionOn the technical side, the in-memory backup is realized through Python modulespickle,multiprocessingandcollections, which are used to serialize and deserialize various types of data and provide the dictionary-like data type that is available in both the guarded application and the rescue handler process.
The Python modulesignalis used to ensure signals like keyboard interrupts are handled correctly and received by the guarded process.
The autosave functionality uses the Python modulelzmafor efficient compression of autosave files,osfor file handling andtimefor measuring the time since a process corresponding to a backup file was last active.Feel encouraged to look into the source code and to contribute through (well documented :D ) pull requests!Faultguard is tested on Linux and Windows.InstallationThis module is available viapip install faultguardor can be installed manually viasetup.py, e.g. downloading the source code and runningpip install .inside the downloaded folder.DisclaimerIf a crash is observed frequently or reproducibly, it should be diagnosed – e.g. withfaulthandler(another Python module) andgdb. If you somehow manage to generate a segmentation fault in thefaultguarddata dictionary, and therefore destroy the guard process, the rescue will of course not work. Preventing faults from happening in the first place is always the most important, so don't rely solely on this module, just use it as an additional safety net!CreditThis project was initially developed for a hardware project at the University of Münster.