问题
News are stored in database MySQL with datetime
of publication.
Any time user can add a new in the table with delay date publishing.
How to use Celery for listeting database table and check if it is time to publish data?
For publishing data is responsible another process(script Python). So Celery should call this script for each rows in MySQL table according datetime
.
How to do that using Celery?
Another way I think to create queue with date
and id
of publication then directly add data from user form in this queue. Therefore sholud be process(Celery) that observes a queue for further executing tasks by date.
Celery provides documentation for Periodic tasks and Scheduled tasks, but there is no explanation and example how to use it.
Sorry if question is trivial, I am a newcomer in Python Celery.
回答1:
you can execute tasks by using paramater eta
on apply_async()
(refer to http://docs.celeryproject.org/en/latest/userguide/calling.html#eta-and-countdown)
eta parameter must be in datetime format, so it will be run on the exact millisecond precision.
I recommend that you use ORM, so the datetime data type from your database will convert automatically by ORM :D
let's say we have an Article model:
class Article(models.Model):
title = models.CharField(max_length=100)
published = models.BooleanField(default=False)
created_date = models.DateTimeField()
and then our tasks module:
@app.task(bind=True)
def set_published(*args, **kwargs):
article = Article.objects.get(id=args[1])
article.published = True
article.save()
print("article %d has been published" % args[1])
Save the new article and call set_published with ETA+1minute
article = Article(title='This is a new article', published=False, created_date=datetime.utcnow())
article.save()
delta = timedelta(minutes=1)
set_published.apply_async(args=(article.id,), eta=article.created_date + delta)
来源:https://stackoverflow.com/questions/47489274/how-to-execute-tasks-in-celery-using-datetime-from-mysql