I am going through a block of legacy code (Python2.6 and Django1.3.3) to acquire lock. Backend is Oracle.
Refer quoted block of code. What is the need for len(query set) on line number 8? I am particularly puzzled due to comment in line number 7. This particular line is consuming time in iterating through large number of records in query set. I am trying to know the reason to have this line before removing it.
I will test this code and update here. I will try without this line and also try replace len with .count() method.
lock_acquired = False
while not lock_acquired:
try:
where_clause= "is_deleted=0"
avail_emp = Employee.objects.select_for_update(nowait=True).extra(where=[where_clause])
log.debug("Acquiring lock")
# *****Please dont remove below log to handle multiple requests at a time while rows are locked by a request*****
log.debug(len(avail_emp))
lock_acquired = True
log.debug("Lock acquired")
except Exception, e:
log.debug("%s - Waiting for 2 secs before retry " %str(e))
sleep(2)