我在使用 Celery 任务从 CSV 中拯救新客户时遇到死锁。这就是我迄今为止的工作。
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,
)
没有错误。
但是,当将相同的代码添加到 Celery 任务时,我收到以下错误......
尝试获取锁定时发现死锁;请尝试重新启动事务
所以,这让我相信我在这里的芹菜设置做错了什么。谁能发现什么?
这是给出死锁错误的新 Celery 任务。我正在使用shared_task
因为这些任务将在某个时候在没有 Django 的不同机器上运行,但现在这无关紧要。
CSV导入中的第一行正常,然后我收到死锁错误...
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)
这是客户经理:
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)
您的代码看起来没有错,但您可能会因为多个芹菜工作线程的并发性而陷入僵局。从 http://celery.readthedocs.org/en/latest/faq.html#mysql-is-throwing-deadlock-errors-what-can-i-do:
MySQL 的默认隔离级别设置为 REPEATABLE-READ(如果您不这样做) 确实需要它,将其设置为读取提交。您可以通过添加 以下内容到您的 my.cnf:
[mysqld] transaction-isolation = READ-COMMITTED