I am writing a gnuradio sink block for custom SDR hardware. When the gnuradio program is closed, I need to make sure that the power amplifiers are disabled (as they draw quite a bit of power and produce a lot of heat). I tried doing this with a class destructor thinking it would be called upon program termination, however it was not. Does gnuradio provide a way to run cleanup upon program termination?
Asked
Active
Viewed 457 times
2 Answers
2
You could overload the stop
method of the gr::block
base class. It's meant for exactly that!

Marcus Müller
- 34,677
- 4
- 53
- 94
1
Example of overriding in Python: if you insert a Python block, add the stop
method like so:
class blk(gr.sync_block):
def __init__(self):
# omitted...
def work(self):
# omitted...
def stop(self):
print "do stopping work here"
return True
Notes:
I don't know what the return value is used for.(Answered in comments -- thanks!)- In my experimentation, it appears that
stop
does not run for blocks that lack inputs and outputs.

pianoJames
- 454
- 1
- 5
- 14
-
1blocks that aren't part of the flow graph don't "run", they only exist (you might have nothing from the fact they're blocks – maybe you'd just want a normal Python class of sorts?). So, that `stop` isn't run if your block isn't connected to something is no surprise! – Marcus Müller Sep 16 '21 at 16:03
-
1the return value of stop() is currently unused. – Marcus Müller Sep 16 '21 at 16:07
-
Very helpful, thanks! Isn't the block's `__init__` run always (even for blocks that are not part of the flow graph)? – pianoJames Sep 21 '21 at 19:05
-
1`__init__` is the constructor. Whenever you instantiate an object of your class, the constructor is called – this is python, not GNU Radio-related. – Marcus Müller Sep 21 '21 at 19:29