我正在使用Django应用。我有一个API端点,如果需要,它必须执行一个必须重复几次的函数(直到某个条件为真)。我现在如何处理它是 -
def shut_down(request):
# Do some stuff
while True:
result = some_fn()
if result:
break
time.sleep(2)
return True
虽然我知道这是一种可怕的方法,而且我不应该在2秒内阻挡,但我无法弄清楚如何绕过它。
在等了4秒之后,这有效。但是我喜欢能让循环在后台运行的东西,并且一旦some_fn返回True就停止。 (另外,some_fn肯定会返回True)。
编辑 -
阅读Oz123的回复给了我一个似乎有用的想法。这就是我的所作所为 -
def shut_down(params):
# Do some stuff
# Offload the blocking job to a new thread
t = threading.Thread(target=some_fn, args=(id, ), kwargs={})
t.setDaemon(True)
t.start()
return True
def some_fn(id):
while True:
# Do the job, get result in res
# If the job is done, return. Or sleep the thread for 2 seconds before trying again.
if res:
return
else:
time.sleep(2)
这对我有用。这很简单,但我不知道多线程与Django的结合效率如何。
如果有人能指出这方面的陷阱,那么批评就会受到赞赏。
答案 0 :(得分:14)
对于许多小项目celery来说太过分了。对于那些您可以使用schedule的项目,它非常易于使用。
使用此库,您可以使任何函数定期执行任务:
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
while True:
schedule.run_pending()
time.sleep(1)
该示例以阻塞方式运行,但是如果您查看常见问题解答,您会发现您还可以在并行线程中运行任务,这样您就不会阻塞,并且不再需要删除任务:< / p>
from schedule import Scheduler
def run_continuously(self, interval=1):
"""Continuously run, while executing pending jobs at each elapsed
time interval.
@return cease_continuous_run: threading.Event which can be set to
cease continuous run.
Please note that it is *intended behavior that run_continuously()
does not run missed jobs*. For example, if you've registered a job
that should run every minute and you set a continuous run interval
of one hour then your job won't be run 60 times at each interval but
only once.
"""
cease_continuous_run = threading.Event()
class ScheduleThread(threading.Thread):
@classmethod
def run(cls):
while not cease_continuous_run.is_set():
self.run_pending()
time.sleep(interval)
continuous_thread = ScheduleThread()
continuous_thread.setDaemon(True)
continuous_thread.start()
return cease_continuous_run
Scheduler.run_continuously = run_continuously
以下是在类方法中使用的示例:
def foo(self):
...
if some_condition():
return schedule.CancelJob # a job can dequeue it
# can be put in __enter__ or __init__
self._job_stop = self.scheduler.run_continuously()
logger.debug("doing foo"...)
self.foo() # call foo
self.scheduler.every(5).seconds.do(
self.foo) # schedule foo for running every 5 seconds
...
# later on foo is not needed any more:
self._job_stop.set()
...
def __exit__(self, exec_type, exc_value, traceback):
# if the jobs are not stop, you can stop them
self._job_stop.set()
答案 1 :(得分:3)
此答案在Oz123's answer上有所扩展。
为了使事情顺利进行,我创建了一个名为mainapp/jobs.py
的文件,其中包含计划的作业。然后,在我的apps.py
模块中,将from . import jobs
放在ready
方法中。这是我的整个apps.py
文件:
from django.apps import AppConfig
import os
class MainappConfig(AppConfig):
name = 'mainapp'
def ready(self):
from . import jobs
if os.environ.get('RUN_MAIN', None) != 'true':
jobs.start_scheduler()
(RUN_MAIN
检查是因为python manage.py runserver
runs the ready
method twice(在两个进程中每个都一次,但是我们只希望运行一次)。
现在,这是我放入jobs.py
文件中的内容。一是进口。您需要如下导入Scheduler
,threading
和time
。 F
和UserHolding
的导入恰好适合我的工作;您将不会导入这些。
from django.db.models import F
from schedule import Scheduler
import threading
import time
from .models import UserHolding
接下来,编写您要安排的功能。以下仅是示例;您的函数看起来不会像这样。
def give_admin_gold():
admin_gold_holding = (UserHolding.objects
.filter(inventory__user__username='admin', commodity__name='gold'))
admin_gold_holding.update(amount=F('amount') + 1)
接下来,通过向schedule
类添加run_continuously
方法来对Scheduler
模块进行猴子补丁。通过使用下面的代码来做到这一点,该代码是从Oz123的答案中逐字复制的。
def run_continuously(self, interval=1):
"""Continuously run, while executing pending jobs at each elapsed
time interval.
@return cease_continuous_run: threading.Event which can be set to
cease continuous run.
Please note that it is *intended behavior that run_continuously()
does not run missed jobs*. For example, if you've registered a job
that should run every minute and you set a continuous run interval
of one hour then your job won't be run 60 times at each interval but
only once.
"""
cease_continuous_run = threading.Event()
class ScheduleThread(threading.Thread):
@classmethod
def run(cls):
while not cease_continuous_run.is_set():
self.run_pending()
time.sleep(interval)
continuous_thread = ScheduleThread()
continuous_thread.setDaemon(True)
continuous_thread.start()
return cease_continuous_run
Scheduler.run_continuously = run_continuously
最后,定义一个函数来创建一个Scheduler
对象,连接您的工作,并调用调度程序的run_continuously
方法。
def start_scheduler():
scheduler = Scheduler()
scheduler.every().second.do(give_admin_gold)
scheduler.run_continuously()
答案 2 :(得分:1)