I have a piece of code that is oftentimes run by multiple processes, in which a certain blobfile has to be updated with some new information. Because I do not want these processes to simultaneously write to this file - which would cause conflict errors - I put a lease on the file when a process starts modifying it, and want to break this lease once it is done modifying it.
Now the problem is that I have to check for each process if there is already a lease on the blobfile, and if not, keep waiting until the lease is broken and then acquire a lease for the current process. The way in which I realise this now is by running the following code:
stream = io.BytesIO()
if self.blob_service.exists(self.container, self.changelog):
while True:
try:
self.blob_service.acquire_blob_lease(self.container, self.changelog,
lease_duration=-1)
break
except AzureConflictHttpError:
pass
self.blob_service.get_blob_to_stream(self.container, self.changelog, stream)
stream.seek(0)
.... do some processing and write to blobfile
self.blob_service.break_blob_lease(self.container, self.changelog)
blob_service
here is an instance of BlockBlobService
. However this results in a continuous stream of logging of 2020-04-19 16:19:51,517 ERROR Client-Request-ID=d53b7e6e-8248-11ea-a073-dca90492a6b8 Retry policy did not allow for a retry: Server-Timestamp=Sun, 19 Apr 2020 14:19:50 GMT, Server-Request-ID=c339439f-e01e-0058-1d55-16e3bc000000, HTTP status code=409, Exception=There is already a lease present. ErrorCode: LeaseAlreadyPresent<?xml version="1.0" encoding="utf-8"?><Error><Code>LeaseAlreadyPresent</Code><Message>There is already a lease present.RequestId:c339439f-e01e-0058-1d55-16e3bc000000Time:2020-04-19T14:19:51.5047575Z</Message></Error>.
This will go on until process 1 breaks the lease and process 2 can acquire the lease.
Now of course there are ways to suppress this logging, but I would much rather be able to do something like this.
while True:
if self.blob_service.blob_has_lease(self.container, self.changelog):
continue
else:
self.blob_service.acquire_blob_lease(self.container, self.changelog,
lease_duration=-1)
In if self.blob_service.blob_has_lease(self.container, self.changelog)
is something that I have made up myself. However I can not in any way find such a method or property for checking if a certain blob has a lease on it currently. Is there any such method or workaround in which I could write my code like the example I have provided above?
You can possibly call get_blob_properties
method to get the properties of a blob. If a blob is leased, then you should get some value in lease
variable under blob's properties
.