2013-03-14 67 views
0

我對Django Celery(使用Amazon SQS)有以下任務。Django芹菜任務如何知道它運行?

@task 
def upload_task(request, **kwargs): 
    file = request.FILES['file'] 
    ContactCSVModel.import_from_file(file) 
    return True 

這似乎工作,即文件已成功添加到我的數據庫,但我不確定它是否使用芹菜。我怎麼知道這是否工作。我應該看到什麼嗎?在我跑終端......

manage.py celery worker --loglevel=info,其中規定我有一個任務叫做contacts.tasks.upload_task,但我從來沒有看到任何事情發生,它只是規定...

[2013年3月14日20:52:47947:信息/ MainProcess]消費者:連接到 SQS:// AJSUQJZKNSJA81JM @本地//

如果我的任務已經運行,並完成了(是的,我知道它完成,因爲它有任何想法是在數據庫中,但是是通過芹菜?)

這是哪裏的任務會從

views.py

@login_required 
def upload(request): 
    # If we had a POST then get the request post values. 
    if request.method == 'POST': 
     form = ContactUploadForm(request.POST, request.FILES) 
     # Check we have valid data 
     if form.is_valid(): 
      upload_task(request) 
      #file = request.FILES['file'] 
      #ContactCSVModel.import_from_file(file) 
      messages.add_message(request, messages.SUCCESS, 'Items have been added') 
     else: 
      messages.add_message(request, messages.ERROR, ' Cannot upload CSV file.') 

      return render_to_response('contacts/home.html', context_instance=RequestContext(request)) 
    else: 
     form = ContactUploadForm() 
     # Do this part no matter the outcome above. 
    context = {'form': form} 
    return render_to_response('contacts/home.html', context, context_instance=RequestContext(request)) 

就像我說的這部作品在加載到數據庫中的CSV數據運行,但我不認爲芹菜做任何事情。

這是我的芹菜設置....

# Celery 
BROKER_TRANSPORT_OPTIONS = {'queue_name_prefix': 'celery-'} 
BROKER_TRANSPORT = 'sqs' 
BROKER_TRANSPORT_OPTIONS = { 
    'region': 'eu-west-1', 
    } 
BROKER_USER = 'xyz' 
BROKER_PASSWORD = 'xyz' 
+2

你可以分享你輸入的內容來運行任務嗎?也就是說,如果你嘗試在shell中運行它,你輸入的行... – 2013-03-14 21:07:40

+0

當然,只是更新:) – jason 2013-03-14 21:11:08

回答

4

您無法看到任務的證據,您的芹菜工人日誌的原因是因爲你沒有實際調用你的任務是異步的。在您的views.py中,而不是upload_task(request),請嘗試upload_task.delay(request),然後檢出工作日誌。爲了使任務修飾函數異步運行,您必須使用.delay()或.apply_async()方法。文件中的其他地方涵蓋了here

更一般地說,如果您正在尋找更具視覺吸引力的方式來跟蹤和管理任務,請查看celery flower

+0

謝謝你本傑明,我是新來的,這解釋了很多。 – jason 2013-03-14 21:16:48

+0

沒問題,很樂意幫忙。你喜歡選擇我的答案作爲正確答案嗎(假設你的問題已得到完全解答)? – 2013-03-14 21:19:15

+1

感謝您的花鏈接,這是我將看到的,是非常有用的。 – jason 2013-03-14 21:19:35