flask run function every minute
To run a Flask function every minute, you can use a scheduler library called "APScheduler" in Flask. Here are the steps to achieve this:
Install APScheduler: Begin by installing the APScheduler library using pip, the package installer for Python. You can do this by running the following command in your command prompt or terminal:
pip install apscheduler
Import necessary modules: In your Flask application file, import the required modules for APScheduler:
python from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.interval import IntervalTrigger
Create a function: Define the function that you want to run every minute. For example, let's say you want to print "Hello, World!" every minute. You can create a function like this:
python def print_hello(): print("Hello, World!")
Initialize the scheduler: Create an instance of the BackgroundScheduler and set it to run in the background:
python scheduler = BackgroundScheduler() scheduler.start()
Schedule the function: Use the scheduler instance to schedule the function to run every minute. You can do this by adding the following code:
python scheduler.add_job(func=print_hello, trigger=IntervalTrigger(minutes=1))
Run the Flask application: Finally, run your Flask application using the
flask runcommand. The scheduler will start running in the background and execute the specified function every minute.
That's it! With these steps, you can run a Flask function every minute using APScheduler. Remember to customize the function and its interval according to your specific requirements.