Actually the problem is very common. Let me describe this by example.
Imagine you have a web service where users register and pay for some gold status. There is expiration date. The question is how to remove the gold status in time?
I see at least two solutions:

  1. Write a daemon that checks user statuses and current time and perform some actions
  2. Use cron as a deamon and do the same

But I believe there are other approaches. By the way the language the system is written is Python but I don't think this really matters.

  • 1
    Whenever you check the status check the expiration date too? – psr Oct 17 '14 at 21:47
  • @psr Yes, of course. When I wrote "check statuses" I meant the experation date if I needed for a system. But I asked for approaches to get this problem solved. I see cron way is ugly. – Deck Oct 17 '14 at 21:52
  • 2
    I don't think it's clear what your problem actually is. If you only consider them to have gold status if expiration Date>= now then there is no moment you need to "remove" it. So what exactly is the problem that remains? – psr Oct 17 '14 at 21:56
  • 1
    Why was my question closed as too broad? Please note: as of the time I am writing this, your question has not been closed. – Adam Zuckerman Oct 18 '14 at 2:05

Assuming you're using a database to store account information, filtering a search on "account_expiry>=get_date()" on the database (i.e. in SQL) is a negligible operation.

If you're not using a database for account information, your approach would vary based on what you're using.

To answer your titled question, how do you run a deferred task on a backend in python, look at Celery

|improve this answer|||||
  • I know about Celery but never used it. The line from docs "It’s a task queue with focus on real-time processing" made me wonder if it suits my needs. For example, if user pays and gets gold status for year. Is it reliably to add a tasks 1. Send email now+year-10day and 2. Remove gold status now+year ? – Deck Oct 17 '14 at 22:38
  • Yes that is an option with celery. You can schedule a job to run a year from now. But it also lets you run jobs with more precision and scale them out and have a single job server/cluster serving a farm of applications. – Michael Brown Oct 17 '14 at 22:53

I believe there are two basic approaches:

  • check the current status every time it's necessary
  • have a process run at some regular interval to do the checking

The problem with the first method is it can get resource intensive, while the problem with the second is that there may be a window where the user shouldn't have the "gold status" but does.

For the second option whether you use cron or a custom deamon or a custom timed method in the program itself really makes no difference.

|improve this answer|||||
  • Thank you for reply. You are right. I asked this question and hoped that the answer would be like "Man, there is the solution that everybody uses. It's called X". Because I think the problem I described exists in every system with "billing" subsystem. If no, everybody solves this differently, Ive got it! – Deck Oct 17 '14 at 22:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.