2013-02-25 45 views
0

我有一个django芹菜视图,它执行某些任务,并在任务完成后成功将其写入数据库。result.ready()在django芹菜中无法正常工作?

我这样做:

result = file.delay(password, source12, destination) 

而且,

if result.successful() is True: 
     #writes into database 

但是任务已完成执行后不进入,如果condition.I试图用result.ready()但没有运气。

编辑:上述线路都在同样的观点:

def sync(request): 
    """Sync the files into the server with the progress bar""" 
    choice = request.POST.getlist('choice_transfer') 
    for i in choice: 
     source12 = source + '/' + i 
     start_date1 = datetime.datetime.utcnow().replace(tzinfo=utc) 
     start_date = start_date1.strftime("%B %d, %Y, %H:%M%p") 

     basename = os.path.basename(source12) #Get file_name 
     extension = basename.split('.')[1] #Get the file_extension 
     fullname = os.path.join(destination, i) #Get the file_full_size to calculate size 

     result = file.delay(password, source12, destination) 

     if result.successful() is True: 
      #Write into database 

E: #Writes数据库

+0

在哪里这两条线生活在关系到彼此? – 2013-02-25 08:52:16

+0

他们都属于同一观点。 – pynovice 2013-02-25 08:54:10

回答

1
  1. 当你调用file.delay,芹菜进行排队任务运行在后台,在稍后的一点。

  2. 如果您立即检查result.successful(),它将是错误的,因为任务尚未运行。

如果需要链的任务(一个接一个烧制)使用芹菜的工作流程解决方案(在这种情况下chain):

def do_this(password, source12, destination): 
    chain = file.s(password, source12, destination) | save_to_database.s() 
    chain() 


@celery.task() 
def file(password, source12, destination): 
    foo = password 
    return foo 


@celery.task() 
def save_to_database(foo): 
    Foo.objects.create(result=foo) 
+0

所以我想要做的是:任务完成后,我想写入一些信息到数据库中。我怎样才能做到这一点? – pynovice 2013-02-25 08:56:50

+0

将代码直接添加到将信息写入数据库的任务,或直接调用任务,并阻塞直到完成。 – 2013-02-25 08:58:57

+0

第一个我做到了!但我不想将排队的任务显示给用户。如果我想用第二个,我永远不会去django芹菜。 – pynovice 2013-02-25 09:01:11