[FIXED] How can I perform some stuff every 60 seconds in Python Flask, and then update all user pages?

Issue

I’m currently working on a turn-based online web game. I want all players to be able to choose a move, and every 60 seconds I want all players to perform their chosen move at the same time (if no move is chosen, the default move is It will be executed). I plan to store the selected moves in a SQL database. The only problem is I don’t know how to do something every 60 seconds.

I tried to use asyncio and I read that Celery helps, but I’m not quite sure how to achieve exactly the result I want. As I understand the guide only helps to use things within the flask app and not at the same time as the flask app

Solution

You can use threading:

import threading
import time

def perform_task():
    print("Performing task")

def schedule_task():
    while True:
        perform_task()
        time.sleep(60)

thread = threading.Thread(target=schedule_task)
thread.start()

Answered By – Abdulmajeed

Answer Checked By – Pedro (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published