Skip to content

Commit 7747088

Browse files
author
David Erb
committed
use asyncio rlock for droplocation upsert
1 parent 786fcc9 commit 7747088

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/xchembku_lib/datafaces/direct.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ class Direct(
2929
# ----------------------------------------------------------------------------------------
3030
def __init__(self, specification=None):
3131
DirectBase.__init__(self, specification)
32+
DirectCrystalWellDroplocations.__init__(self, specification)

src/xchembku_lib/datafaces/direct_crystal_well_droplocations.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import asyncio
12
import copy
23
import logging
3-
from threading import RLock
44
from typing import Dict, List, Optional
55

66
from dls_normsql.constants import CommonFieldnames
@@ -14,13 +14,16 @@
1414

1515
logger = logging.getLogger(__name__)
1616

17-
# Module-level lock to keep upsert atomic.
18-
upsert_lock = RLock()
19-
2017

2118
class DirectCrystalWellDroplocations(DirectBase):
2219
""" """
2320

21+
# ----------------------------------------------------------------------------------------
22+
def __init__(self, specification=None):
23+
24+
# Lock allows only one coroutine to acquire it at a time.
25+
self.__upsert_lock = asyncio.Lock()
26+
2427
# ----------------------------------------------------------------------------------------
2528
async def upsert_crystal_well_droplocations_serialized(
2629
self,
@@ -104,8 +107,10 @@ async def upsert_crystal_well_droplocations(
104107

105108
# Loop over all the models to be upserted.
106109
for model in models:
107-
# TODO: Reconsider the lock granularity in direct_crystal_well_droplocations.py.
108-
with upsert_lock:
110+
# Need to lock because of possible long time between query and eventual insert, user might double-click to send two droplocations quite quickly.
111+
# TODO: Reconsider direct_crystal_well_droplocations upsert logic to be tolerant of multiple processes possibly doing concurrent insert.
112+
# TODO: Add unit test for concurrent direct_crystal_well_droplocations upsert.
113+
async with self.__upsert_lock:
109114
model_dict = copy.deepcopy(model.dict())
110115

111116
# Find any existing record for this model object.

0 commit comments

Comments
 (0)