This example demonstrates the Distributed Lock component APIs in Dapr. It demonstrates the following APIs:
- try_lock: Attempts to acquire a distributed lock from the lock store.
- unlock: Attempts to release (a previously acquired) distributed lock
It creates a client using DaprClient
, uses a local lock store defined in
./components/lockstore.yaml
and invokes
all the distributed lock API methods available as example.
pip3 install dapr dapr-ext-grpc
To run this example, the following code can be utilized:
dapr run --app-id=locksapp --app-protocol grpc --resources-path components/ python3 lock.py
The output should be as follows:
== APP == Will try to acquire a lock from lock store named [lockstore]
== APP == The lock is for a resource named [example-lock-resource]
== APP == The client identifier is [example-client-id]
== APP == The lock will will expire in 60 seconds.
== APP == Lock acquired successfully!!!
== APP == We already released the lock so unlocking will not work.
== APP == We tried to unlock it anyway and got back [UnlockResponseStatus.lock_does_not_exist]
The Dapr python-sdk will pass through errors that it receives from the Dapr runtime.