Reputation: 443
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
Upvotes: 2
Views: 521
Reputation: 5492
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
Upvotes: 3