'Is there a way to stop @repeat_every decorator in FastAPI

I need to create a continuous loop to one of the class Attribute in FastAPI.

For eg.,

class Exampl:
    

    def __init__(self, id_, connect_url='', status='Down'):
        """Initialize a new server"""

        self.id_ = id_
        self.connection_url = connection_url 
        self.status = status
        
        self.server_is_down = \
            LoopingCall(self._check_if_server_is_down)
        self.server_is_down .start(3)

The above code says LoopingCall which will be under continuous loop. So we have start() and stop() for that. It belongs to twisted framework.

Now we want that to be done in FastAPI. I'm trying to achieve the same in FastAPI.

My Findings:

  1. I found repeat_every decorator from https://fastapi-utils.davidmontague.xyz/user-guide/repeated-tasks/ . Here I can start the method but how can I stop ?

  2. Can we achieve the same with asyncio.get_event_loop() without stopping the Fastapi uvicorn ?

  3. Or is there any way to achieve this challenge ? I just want a method to run every x seconds. I want to stop the method when I want to.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source