Open
Description
The goal I'm trying to accomplish is a class that cleans up after itself when the object is no longer in use. Users (i.e: me) sometimes forget to clean up their own messes.
__del__
seemed to be the right way to implement this, but it isn't working as expected.
I have the following module (test_helpers.py
):
class NoisyThing:
def __init__(self, name):
log.info(f'starting NoisyThing {name}')
self.name = name
self.counter = 0
self.trigger_func = None
self.startup()
@pyscript_compile
def __del__(self):
self.trigger_func = None
def startup(self):
@time_trigger("period(now, 1sec, now + 30sec)")
def be_noisy():
self.counter = self.counter + 1
log.info(f'{self.name} counter {self.counter}')
self.trigger_func = be_noisy
If used like this, by reloading (just saving the file again with no changes) you can see by the log output the old object and trigger functions are not destroyed. Reloading test_helpers
, however, does stop the noise:
from test_helpers import NoisyThing
a = NoisyThing('a')
Ideally, the above would work as the goal is to make NoisyThing
clean up after itself. However, this (which doesn't accomplish the goal) also doesn't work:
from test_helpers import NoisyThing
a = NoisyThing('a')
@time_trigger('shutdown')
def shutdown():
# without global a "name 'a' is not defined" NameError occurs
global a
del a
The only way to make it work is to call it directly:
from test_helpers import NoisyThing
a = NoisyThing('a')
@time_trigger('shutdown')
def shutdown():
a.__del__()
Metadata
Metadata
Assignees
Labels
No labels