A
A
Alexander2021-12-15 22:50:08
Django
Alexander, 2021-12-15 22:50:08

How to start a Django background process?

What is the best way to run background processes in django?
The bottom line is that I have a post on the site, it has a deactivation date, I need to deactivate it in real time at the moment when the end date coincides with the current date or will already be "in the past". Regarding the implementation of background processes in python, I found only this example

import time
from threading import Thread

def timer(name):
    count = 0
    while True:
        time.sleep(60)
        count += 1            
        print("Hi " + name + "This program has now been running for " + str(count) + " minutes.")

print("Hello! Welcome to the program timer!")
name = raw_input("What is your name?")
print("Nice to meet you, " + name + "!")
background_thread = Thread(target=timer, args=(name,))
background_thread.start()

But the question arose, regarding the fact that I can have an unlimited number of posts. Theoretically, the function itself could be called in the background and it would "run through" all the records of posts in the database, take the date of deactivation, and compare it with the current one, if the current one is "greater", then it would remove it. Or, with the creation of each post, start the process separately, but I suspect that the idea is not very ...
In short, is there a solution more adequate and correct for my purposes (and in general from the point of view of background processes) than what I described higher?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2021-12-15
@AlexDolls

Learning Python/Django and everything that accompanies it in web development, such as Django Rest Framework, Django Channels, PostgreSQL, Redis, Docker...

add Celery to this set,
but in your case, cron/systemd/supervisord is enough

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question