django - Concurency celery django 批量创建或更新
问题描述
我有一个 django 的竞争问题,我有一个更新表中用户数量的 celery 任务。由于要更新的行很多,我使用批量更新,为了避免创建不必要的行,我使用批量创建。
all_statistic_user = StatisticsCountriesUser.objects.filter(user__in=all_branch_user,countries=user.country)
if all_statistic_user:
all_statistic_user.update(total_users=F('total_users') +1)
all_statistic_user = StatisticsCountriesUser.objects.exclude(user__in=all_branch_user,countries=user.country)
if all_statistic_user:
all_statistic_user = [StatisticsCountriesUser(user_id=user.id,countries=user.country) for user in all_statistic_user ]
StatisticsCountriesUser.objects.bulk_create(all_statistic_user)
else:
all_statistic_user = [StatisticsCountriesUser(user_id=user.id, countries=user.country) for user in all_branch_user]
StatisticsCountriesUser.objects.bulk_create(all_statistic_user)
问题是如果任务是异步执行的,如果任务同时执行第一个任务创建用户列表,那么第二个任务也检索用户列表,第一个创建或更新用户,但是第二个任务该列表不再具有更新或创建的正确信息。我认为的解决方案是将任务放在同步列表中而不是异步的,这可能吗?先感谢您
解决方案
推荐阅读
- c++ - 如何仅从类类型推断类构造函数参数的类型
- javascript - 如何根据从下拉菜单中选择的选项设置不同的输入字段模式?
- php - 为什么我不能在 wordpress 中使用 $wpdb
- javascript - 将输入转换为小写
- angular - (change) $event 不起作用,可能是语法问题
- java - 将 EditText 背景设置为透明后闪烁的光标消失
- angular - 带有下拉菜单的 Angular 8 文本输入
- arrays - 如何在json中仅获取Array部分
- python - sqlite3 和 tkinter 中的用户名和密码变量问题
- sql-server-express - 如何访问 SQL Server?