1
我想用芹菜插入大數據在我的mongodb,但問題是併發在MongoDB中。如果我一次發送多個任務到芹菜,一部分數據將被插入到mongodb中,而另一些數據則不會。我想這是因爲mongodb在插入操作時鎖定了數據庫,但我需要一種解決方案來發送相同類型的多個任務以在數據庫中插入數據。像檢查數據庫是否被鎖定,如果它正在等待它解鎖。這裏是我的代碼的一部分:如何使用芹菜插入數據到mongodb使用mongoengine
@celery.task(name='celery_tasks.add_book_product')
def add_book_product(product_dict, store_id):
connect(DefaultConfig.MONGODB_DB, host=DefaultConfig.MONGODB_HOST)
store_obj = Store.objects.get(pk=store_id)
try:
book = Books.objects.get(pk=product_dict['RawBook'])
try:
product_obj = Product.objects.get(store=store_obj, related_book=book, kind='book')
print("Product {} found for store {}".format(product_obj.id, store_obj.id))
product_obj.count = int(product_dict['count'])
product_obj.buy_price = int(product_dict['buy_book'])
product_obj.sell_price = int(product_dict['sell_book'])
product_obj.save()
except (DoesNotExist, ValidationError):
product_obj = Product(store=store_obj,
related_book=book,
kind='book',
count=int(product_dict['count']),
buy_price=int(product_dict['buy_book']),
sell_price=int(product_dict['sell_book']),
name=book.name_fa)
product_obj.save()
print("Appending books to store obj...")
store_obj.products.append(product_obj)
store_obj.save()
print("Appending books to store obj done")
return "Product {} saved for store {}".format(product_obj.id, store_obj.id)
except (DoesNotExist, ValidationError):
traceback.print_exc()
return "Product with raw book {} does not exist.".format(product_dict['RawBook'])