Prometheus
Prometheus

Reputation: 33625

Celery Deadlock when saving data in Django, why?

I have a Deadlock when using a Celery task to save new customers from a CSV. This is what I have working so far.

  for line in csv.reader(instance.data_file.read().splitlines()):
        for index, item in enumerate(line):
            number = int(item)
            # TODO: Turn into task
            Customer.objects.create_customer(
                mobile=number,
                campaign=instance.campaign,
                reward_group=instance.reward_group,
                company=instance.company,
            )

No errors.

However, when add this same code to a Celery task I get the following error...

Deadlock found when trying to get lock; try restarting transaction'

So, this leads me to believe that I have done something wrong with my celery setup here. Can anyone spot what?

Here is the new Celery task that gives the deadlock error. I'm using shared_task as these task will at some point run on a different machine without Django, but that should not matter for now.

The first row in the CSV import ok, then I get a deadlock error...

for line in csv.reader(instance.data_file.read().splitlines()):
    for index, item in enumerate(line):
        number = int(item)
        celery_app.send_task('test.tasks.create_customer_from_import', args=[number, instance.id], kwargs={})

tasks.py

# Python imports
from __future__ import absolute_import

# Core Django imports
from celery import shared_task

from mgm.core.celery import app as celery_app

@shared_task
def create_customer_from_import(number, customer_upload_id):
    customer_upload = CustomerUpload.objects.get(pk=customer_upload_id)
    new_customer = Customer.objects.create_customer(
        mobile=number,
        campaign=customer_upload.campaign,
        reward_group=customer_upload.reward_group,
        company=customer_upload.company,
    )
    return new_customer

celery.py

from __future__ import absolute_import

import os

from celery import Celery

from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test.settings')

app = Celery('test-tasks')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

This is the CustomerManager:

class CustomerManager(models.Manager):
    def create_customer(self, mobile, campaign, reward_group, company, password=None):.
        user = AppUser.objects.create_user(mobile=mobile)
        # Creates a new customer for a company and campaign
        customer = self.model(
            user=user,
            campaign=campaign,
            reward_group=reward_group,
            company=company
        )

        customer.save(using=self._db)

Upvotes: 4

Views: 2704

Answers (1)

dukebody
dukebody

Reputation: 7185

Your code doesn't look wrong, but you're probably getting the deadlock because of the concurrency of multiple celery workers. From http://celery.readthedocs.org/en/latest/faq.html#mysql-is-throwing-deadlock-errors-what-can-i-do:

MySQL has default isolation level set to REPEATABLE-READ, if you don’t really need that, set it to READ-COMMITTED. You can do that by adding the following to your my.cnf:

[mysqld]
transaction-isolation = READ-COMMITTED

Upvotes: 4

Related Questions