Django - run a function every x seconds

后端 未结 3 1848
梦毁少年i
梦毁少年i 2020-12-23 12:00

I\'m working on a Django app. I have an API endpoint, which if requested, must carry out a function that must be repeated a few times (until a certain condition is true). Ho

3条回答
  •  攒了一身酷
    2020-12-23 12:50

    I recommend you use Celery's task management. You can refer this to set up this app (package if you're from javaScript background).

    Once set, you can alter the code to:

    @app.task
    def check_shut_down():
        if not some_fun():
            # add task that'll run again after 2 secs
            check_shut_down.delay((), countdown=3)
        else:
            # task completed; do something to notify yourself
            return True
    

提交回复
热议问题