代码之家  ›  专栏  ›  技术社区  ›  Bak

如何安排作业(Django、Python)

  •  0
  • Bak  · 技术社区  · 7 年前

    我想创造一个工作机会,让所有10种弹药都能使用。 我发现了一个很好的例子 here .问题是程序在等待期间冻结,我的其他URL被阻止。 在我之后是因为 尽管如此:

    有没有一种不绕过这个问题的方法?

    voici le代码:

    import schedule
    import time
    
    def job():
        print("I'm working...")
    
    schedule.every(10).minutes.do(job)
    
    while True:
        schedule.run_pending()
        time.sleep(1)
    

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

    我找到了正确的方法。这是答案 link : 为了更好地工作,我删除了这个部分:

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

    以下是有效的代码:

    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()
    

    谢谢大家,谢谢作者:帕里斯·中田凯瑟 Here

    1 回复  |  直到 7 年前
        1
  •  3
  •   Ozgur Akcali    7 年前

    您可以将芹菜+芹菜节拍与Django一起使用来运行计划任务。您可以将方法编写为芹菜任务,并在 设置。py 文件,使任务每10分钟运行一次。任务将在其on线程中运行,因此不会阻塞应用程序。

    请看芹菜链接: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html