我必须用django ORM数据做一些长时间(2-3天)的任务。我环顾四周,没有找到任何好的解决办法。
django-任务- http://code.google.com/p/django-tasks/没有很好的文档,我也不知道如何使用它。
芹菜- http://ask.github.com/celery/对我的任务来说是过度的。适合长期任务吗?
所以,我需要做的是,从我的数据库中获取所有数据或部分数据,比如:
Entry.objects.all()然后,我需要对每个QuerySet执行相同的函数。
我认为它应该在2-3天左右起作用。
所以,也许有人会为我解释如何建造它。
P.S:at我只有一个想法,使用cron和数据库来存储进程执行时间线。
发布于 2011-09-11 01:18:33
使用芹菜子-任务。这将允许您启动一个长期运行的任务(它下面有许多短期运行的子任务),并在芹菜的任务结果存储中保存有关它的执行状态的良好数据。作为额外的奖励,子任务将分布在员工过程中,允许您充分利用多核心服务器,甚至多个服务器,以减少任务运行时。
编辑:示例:
import time, logging as log
from celery.task import task
from celery.task.sets import TaskSet
from app import Entry
@task(send_error_emails=True)
def long_running_analysis():
entries = list(Entry.objects.all().values('id'))
num_entries = len(entries)
taskset = TaskSet(analyse_entry.subtask(entry.id) for entry in entries)
results = taskset.apply_async()
while not results.ready()
time.sleep(10000)
print log.info("long_running_analysis is %d% complete",
completed_count()*100/num_entries)
if results.failed():
log.error("Analysis Failed!")
result_set = results.join() # brings back results in
# the order of entries
#perform collating or count or percentage calculations here
log.error("Analysis Complete!")
@task
def analyse_entry(id): # inputs must be serialisable
logger = analyse_entry.get_logger()
entry = Entry.objects.get(id=id)
try:
analysis = entry.analyse()
logger.info("'%s' found to be %s.", entry, analysis['status'])
return analysis # must be a dict or serialisable.
except Exception as e:
logger.error("Could not process '%s': %s", entry, e)
return None 如果不能将计算重新划分为每项任务,则始终可以将其设置为一个子任务执行tallys,一个子任务执行另一个分析类型。这仍然有效,也会让你从平行主义中获益。
https://stackoverflow.com/questions/7373909
复制相似问题