python - how to build dynamic database (autonomously operating) in Django? -
my problem want build django website has periodically-automatically-updated databases @ back-end of django application. example, want add new record database every 5 second, , wrote code in models.py follow:
import datetime django.db import models import time class descriptors(models.model): updated = models.datetimefield(auto_now=true) def add_new_record(): descriptor = descriptors(updated = datetime.datetime.now()).save() time.sleep(5) while true: add_new_record()
this code create record in background when run python manage.py startserver
. however, code stick @ creating record without starting server. there idea how can solve problem, or tutorial should refer making dynamic database @ back-end of django? thank you.
make separate management command that:
# yourapp/management/commands/create_descriptor.py import datetime django.core.management import basecommand class command(basecommand): def handle(self, *args, **kwargs): descriptors.objects.create(updated = datetime.datetime.now())
and line crontab (if you're on unix-compatible system):
* * * * * /path/to/your/manage.py create_descriptor
note, however, vanilla cron can't invoke command quicker every minute, if need higher time resolution may need celery's beat.
Comments
Post a Comment