[Python] How to delete an object?

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am gonna use my codes for image processing and I created OOP programming codes. If self.set_variables.has_changed() trigger, I want to destroy all created objects such as self.set_variables, self.log_cl, self.model_cl, and self.stream_cl.

    from set_variables import SetVariables
    from model_loader import ModelLoader
    from open_stream import VideoStreamHandler
    from log_record import LogRecording
    import sys
    import gc
    from pympler import asizeof

    class ObjectDetector:
    liste = []
    def __init__(self):
    self.reinit()
    self.start_detection()

    def reinit(self):
    gc.collect()
    self.set_variables: SetVariables = SetVariables()
    self.log_cl: LogRecording = LogRecording(self.set_variables)
    self.log_cl.logger_info("INIT STARTED")
    self.model_cl: ModelLoader = ModelLoader(self.set_variables, self.log_cl)
    self.stream_cl: VideoStreamHandler = VideoStreamHandler(self.set_variables, self.log_cl)

    def start_detection(self):
    while True:
    try:

    if self.set_variables.has_changed():
    self.reinit()`your text`


    I tried del keyword but when I checked the size of objects, it increased every time self.set_variables.has_changed() was triggered. Is this normal?


    if self.set_variables.has_changed():
    del self.set_variables
    del self.log_cl
    del self.model_cl
    del self.stream_cl
    self.reinit()
    print("1-self.set_variables",asizeof.asizeof(self.set_variables))
    print("1-self.log_cl",asizeof.asizeof(self.log_cl))
    print("1-self.model_cl",asizeof.asizeof(self.model_cl))
    print("1-self.stream_cl",asizeof.asizeof(self.stream_cl))


    The result is;

    `1-self.set_variables 1552
    1-self.log_cl 6952
    1-self.model_cl 1008440
    1-self.stream_cl 6228352
    [INFO][11/12/2024--14:54]: INIT STARTED
    [INFO][11/12/2024--14:54]: [load_model] [MODEL LOADED SUCCESFULLY]
    [INFO][11/12/2024--14:54]: [open_source] [CAM OPENED SUCCESFULLY]
    1-self.set_variables 1552
    1-self.log_cl 7928
    1-self.model_cl 1009416
    1-self.stream_cl 6229328
    [INFO][11/12/2024--14:54]: INIT STARTED
    [INFO][11/12/2024--14:54]: [load_model] [MODEL LOADED SUCCESFULLY]
    [INFO][11/12/2024--14:54]: [open_source] [CAM OPENED SUCCESFULLY]
    1-self.set_variables 1552
    1-self.log_cl 8936
    1-self.model_cl 1010424
    1-self.stream_cl 6230336`


    So I think that the past object can not be deleted.

    How can I fix it? How can I close objects?

    Continue reading...

Compartilhe esta Página