How to run background tasks in python

14,314
import threading
import time

class BackgroundTasks(threading.Thread):
    def run(self,*args,**kwargs):
        while True:
            print('Hello')
            time.sleep(1)

t = BackgroundTasks()
t.start()

After the while statement , you can put the code you want to run in background. Maybe deleting some models , sending email or whatever.

Share:
14,314
RAM
Author by

RAM

Software developer with a keen interest in numerical analysis and number-crunching in general.

Updated on June 15, 2022

Comments

  • RAM
    RAM almost 2 years

    I'm developing a small web service with Flask which needs to run background tasks, preferably from a task queue. However, after googling the subject the only results were essentially Celery and Redis Queue, which apparently require separate queuing services and thus are options that are far too heavy and convoluted to deploy. As all I'm looking for is a simple background task queue that enables tasks to be queued and executed on separate threads/processes, does anyone know if there is anything like this available in Python?