2

I would like to create a job that rolls to all 10 munites. I find a good example here. The problem is that the program is freezing during the waiting time and my other urls are blocked. after me it's because of while True:

Is there a way to do it without going around this problem?

voici le code:

import schedule
import time

def job():
    print("I'm working...")

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

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

*******************************************************************.

I found the right way to do it. Here is the link: For that to work well, I removed this part:

# time.sleep(20)
# print('Checkpoint **************************')
# time.sleep(30)
# print('Bye -----------------------')

Here is the code that works:

import threading
class ThreadingExample(object):
    """ Threading example class
    The run() method will be started and it will run in the background
    until the application exits.
    """

    def __init__(self, interval=10):
        """ Constructor
        :type interval: int
        :param interval: Check interval, in seconds
        """
        self.interval = interval

        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True                            # Daemonize thread
        thread.start()                                  # Start the execution

    def run(self):
        """ Method that runs forever """
        while True:
            # Do something
            print('Doing something imporant in the background', self.interval)
            pk_info_semaine = job_temp.objects.all()
            for a in pk_info_semaine:
                print('num_semaine:',a.num_semaine,'user_id:',a.user_id)
            time.sleep(self.interval)

example = ThreadingExample()

Thank you all and thank you to the author: Paris Nakita Kejser Here

Bak
  • 411
  • 1
  • 5
  • 19
  • 1
    Maybe the [schedule FAQ](https://schedule.readthedocs.io/en/stable/faq.html#how-to-continuously-run-the-scheduler-without-blocking-the-main-thread) can help you – ritlew Nov 01 '18 at 16:48
  • 1
    Depending on what you are doing you might want to look at other frameworks that were built with django in mind for doing scheduled tasks... It doesn't look like `schedule` is one of them – ritlew Nov 01 '18 at 16:54
  • 1
    The answers in this question are good: https://stackoverflow.com/questions/573618/django-set-up-a-scheduled-job – ritlew Nov 01 '18 at 16:55

1 Answers1

3

You can use celery + celerybeat together with Django to run scheduled tasks. You can write your method as a celery task, and add an entry in your settings.py file to make the task run every 10 minutes. The task will run in its on thread, hence not blocking your application.

voici le link to celery: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

Ozgur Akcali
  • 5,264
  • 2
  • 31
  • 49
  • Thank you, I will follow the instructions. I will get back to you on the final result. – Bak Nov 01 '18 at 20:48