0

I have the following task for Django Celery (using Amazon SQS).

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

This appears to work i.e. the file is successfully added it to my database, but I'm not sure it's using Celery at all. How do I know if this worked. Should I see something? In the terminal I ran...

manage.py celery worker --loglevel=info, which states I have one task called contacts.tasks.upload_task, but I never see anything happen it just states...

[2013-03-14 20:52:47,947: INFO/MainProcess] consumer: Connected to sqs://AJSUQJZKNSJA81JM@localhost//

Any idea's if my task has run and was completed (yes I know it completed due to it being in the database, but was that via Celery?)

this is where the task gets run from

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

like I said this works the csv data in loaded into the database, but I don't think Celery is doing anything.

this is my Celery settings....

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

1 回答 1

5

The reason you're not seeing evidence of the task in your Celery worker log is because you're not actually calling your task asynchronously. In your views.py, instead of upload_task(request), try upload_task.delay(request), and then check out the worker log. In order to have a task-decorated function run asynchronously, you must use either the .delay() or .apply_async() methods. Amongst other places in the documentation, this is covered here.

More generally, if you're looking for a more visually appealing way to track and manage tasks, check out celery flower.

于 2013-03-14T21:14:14.693 回答