Python Function Attributes to Handle Interupt
Although deleted, a comment by @three-d has brought to my attention the atexit module the appears to solve the question of this post from what I am reading. I will have to test it out to determine it actually works as planned, if not then I can look into building a wrapper in C and just letting it save upon crash/shutdown.
From Pixabay
So I know a lot more about C/C++ over python and I had a couple of questions.
Firstly in C/C++ there is something called function attributes, the two attributes I am going to focus on are the constructor and destructor attributes. In C++ a function with the constructor attribute will be started before the main function (making it like an initiation type thing) and the destructor is called after the exit call (which can easily, on many systems, be modified so that it will be called and ran on interrupt calls (like from pressing ctrl+c). This becomes very useful for programs that involve live modification of data, direct kernel interaction, and anything to do with multi-threading. My question is if there is a way to create a function that will always be called upon exit in python (including on interrupt calls) without having to manually import signals and create case point scenario for each of these situations in order to have a function called every time.
For instance, lets say I want to give the function foo some attribute that allows it to run after any exit or interrupt signal.
In C++ foo.h
void foo() __attribute__(destructor, interrupt ("ABORT"))
foo.cpp
#include "foo.h"
void foo(){
//do something
}
Now I know python allows for function attributes using the following syntax:
def foo(bar):
@bar
#do something
and somewhere else you can define the attribute:
def bar(func):
func.is_boo = true
return func
But I was wondering if there were any presets that allow for a function to be automatically called upon receiving an interrupt (such as abort) or exit call to allow us to backup certain data on the fly or would I have to set it up manually so that functions with a specified attribute are called upon requested signals?
Like could I give a function an attribute like destructor
and have it called upon any exit/abort, have another attribute like a constructor
and have it automatically called upon startup (without having to manually call it kind of thing)
I might be wrong with how attributes work in python completely and if that is the case, I apologize.
I am newer to python programming and my reason for getting into it in the first place is for using the steem-python library. I wish there was a library for C++, especially since the witness full node code provided is mostly written in C++ from what I remember. My reason is I want to write a backup function that when a failure, interrupt, or otherwise happens, the function will get called and save the last block watched so that when it starts back up it can start monitoring from that block forward.
Thank you in advance for any help acquired.
Here is my deleted comment, in case it will help someone in the future.
While I do not have much experience in Python. I read about the atexit module on stackoverflow.com and I thought it looks interesting.
https://docs.python.org/3/library/atexit.html
However, I see there are some multi-process uses cases that are not interrupt friendly, and it may not work for your case. I wonder if you can modify the solution below for backup rather than delete?
https://stackoverflow.com/questions/9916301/error-with-multiprocessing-atexit-and-global-data
I hope someone with more experience will chime in. Keep us posted, and good-luck. --3D
Based on the case scenario, since my exit scenario is simply writing to a document it should work on exit scenarios as the multiprocess error you brought up did have a potential fix for case specific scenarios.
I wish I had a larger upvote to offer you for this. To make up for it I am offering you a cut of the profits upon payout.
I am happy it is working out. That is very kind of you. Thanks!