Skip to content
Advertisement

Local Django server unresponsive when adding function scheduling

I am trying to schedule a function to run everyday using the Schedule library.

My local Django server however hangs and becomes unresponsive during the system check after saving the schedules to my code. It is only when I remove the schedule code the system check passes and the server runs without problem.

I have copied the example directly from the documentation and the server is not returning any errors so I am unsure what the problem is ..

views.py

.... 

def test_task():
    user = user.objects.get(pk=1)
    user.task_complete = True
    user.save()

schedule.every(10).minutes.do(test_task)

while True:
    schedule.run_pending()
    time.sleep(1)

....

Terminal output (hangs here)

chaudim@TD app_one % python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Advertisement

Answer

Django loads (imports) files based on its settings.

When you put this while loop in a global scope, it is executed on import. It runs the while loop until it’s done. And it’s never done. You can add a print statement there if you want to see for yourself if that’s the root cause.

Normally people use periodic_tasks from celery but it might be an overkill for your needs.

I’d rather advise to create a command so you could run python manage.py test_task and on the os level just add a cron job that will run this command every 10 minutes.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement