English 中文(简体)
Django:如何以友好方式获得_或_create()?
原标题:Django: how to do get_or_create() in a threadsafe way?

在我的Django,我常常需要做类似于<条码>、植被_或出口()。 例如,

User submits a tag. Need to see if that tag already is in the database. If not, create a new record for it. If it is, just update the existing record.

But looking into the doc for get_or_create() it looks like it s not threadsafe. Thread A checks and finds Record X does not exist. Then Thread B checks and finds that Record X does not exist. Now both Thread A and Thread B will create a new Record X.

这必须是非常常见的情况。 我如何以可怕的方式处理该问题?

最佳回答

This must be a very common situation. How do I handle it in a threadsafe way?

Yes.

“标准”解决办法只是试图创造记录。 如果它发挥作用,那是好的。 持续进行。

如果试图创建记录,则从RDBMS处获得“复制”例外,则采用电子数据并保持运行。

然而,Django有一个ORM层,有自己的藏身。 因此,该逻辑被推倒,无法直接和迅速地处理共同案件,不常见的情况(重复)则引起罕见的例外。

问题回答

自2013年起 或者,获得原产地为原子,因此,它处理的是共同点:

This method is atomic assuming correct usage, correct database configuration, and correct behavior of the underlying database. However, if uniqueness is not enforced at the database level for the kwargs used in a get_or_create call (see unique or unique_together), this method is prone to a race-condition which can result in multiple rows with the same parameters being inserted simultaneously.

If you are using MySQL, be sure to use the READ COMMITTED isolation level rather than REPEATABLE READ (the default), otherwise you may see cases where get_or_create will raise an IntegrityError but the object won’t appear in a subsequent get() call.

http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create”rel=“noreferer”

Here s an example of how you could do it:

1. 界定具有独特性的模式 真:

class MyModel(models.Model):
    slug = models.SlugField(max_length=255, unique=True)
    name = models.CharField(max_length=255)

MyModel.objects.get_or_create(slug=<user_slug_here>, defaults={"name": <user_name_here>})

......或通过使用独一无二的“togheter”:

class MyModel(models.Model):
    prefix = models.CharField(max_length=3)
    slug = models.SlugField(max_length=255)
    name = models.CharField(max_length=255)

    class Meta:
        unique_together = ("prefix", "slug")

MyModel.objects.get_or_create(prefix=<user_prefix_here>, slug=<user_slug_here>, defaults={"name": <user_name_here>})

说明非岛屿田地是如何处于缺省状态的,NOT是独一无二的田地。 这将确保你的创造是原子的。

这里指的是它如何在Django执行:rel=“noreferer”https://github.com/django/django/django/blob/fd60e688c986a102f0125d9dcdcf46pf61fmodgf - 制造物体,捕获最终的正直,并归还该笔物品。 换言之,在数据库中处理原子问题。

多年来,没有人写过<条码>读物。 洛克希德/编码。 如果你没有机会为<条码>unique 和 进行迁徙,那么你会利用锁或<条码>进行翻新。 Semaphore Object. The pseudocode:

from concurrent.futures import ThreadPoolExecutor
from threading import Lock

_lock = Lock()


def get_staff(data: dict):
    _lock.acquire()
    try:
        staff, created = MyModel.objects.get_or_create(**data)
        return staff
    finally:
        _lock.release()


with ThreadPoolExecutor(max_workers=50) as pool:
    pool.map(get_staff, get_list_of_some_data())




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签