MongoDB / Mongoose批量更新竞争条件?

时间:2018-01-03 03:23:37

标签: mongodb mongoose race-condition

说我有以下方法:

export async function markItemsPending (transaction: string, items: string[]) {
  const result = await Inventory.update({
    _id: {$in: items},
    state: InventoryState.Available
  }, {
    state: InventoryState.Pending,
    transaction: transaction
  }, {
    multi: true
  });
  return result.n;
}

并且说这个方法在具有不同事务ID的两个不同线程上同时被调用(比如Tx1Tx2)但是相同的项目(所有这些都是Available之前的Pending调用)。可能发生以下哪种情况?

  1. 所有项目都设置为items.length,具有相同的交易。一个调用返回0,而另一个调用返回Pending
  2. 所有项目均设为transaction。有些人将Tx1设置为transaction,有些人将Tx2设置为items.length。两个函数调用的返回值总和为Pending
  3. 所有项目均设为transaction。有些人将Tx1设置为transaction,有些人将Tx2设置为items.length。两个函数调用的返回值总和为大于 import cv2 import numpy as np import concurrent.futures cap = cv2.VideoCapture(0) while(1): # Take each frame _, frame = cap.read() # Convert BGR to HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_red = np.array([150,150,50],np.uint8) upper_red = np.array([180,255,150],np.uint8) # Threshold the HSV image to get only blue colors #finding the range of red mask = cv2.inRange(hsv, lower_red, upper_red) res = cv2.bitwise_and(frame,frame, mask = mask) kernel = np.ones((5,5),np.uint8) mask = cv2.dilate(mask,kernel) res = cv2.bitwise_and(frame,frame, mask = mask) (_,contours,hierarchy) = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) for pic, contour in enumerate(contours): area = cv2.contourArea(contour) if(area>2000): x,y,w,h = cv2.boundingRect(contour) frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2) cv2.imshow('frame',frame) cv2.imshow('mask',mask) cv2.imshow('res',res) k = cv2.waitKey(5) & 0xFF if k == 27: break cap.release(); cv2.destroyAllWindows() ,表示某些项已被两个方法调用更新。

0 个答案:

没有答案