AutoLockRenewer Class
Auto renew locks for messages and sessions using a background thread pool.
Auto renew locks for messages and sessions using a background thread pool. It is recommended setting max_worker to a large number or passing ThreadPoolExecutor of large max_workers number when AutoLockRenewer is supposed to deal with multiple messages or sessions simultaneously.
- Inheritance
-
builtins.objectAutoLockRenewer
Constructor
AutoLockRenewer(max_lock_renewal_duration: float = 300, on_lock_renew_failure: LockRenewFailureCallback | None = None, executor: ThreadPoolExecutor | None = None, max_workers: int | None = None)
Parameters
Name | Description |
---|---|
max_lock_renewal_duration
|
A time in seconds that locks registered to this renewer should be maintained for. Default value is 300 (5 minutes). Default value: 300
|
on_lock_renew_failure
|
Optional[<xref:LockRenewFailureCallback>]
A callback may be specified to be called when the lock is lost on the renewable that is being registered. Default value is None (no callback). Default value: None
|
executor
|
A user-specified thread pool. This cannot be combined with setting max_workers. Default value: None
|
max_workers
|
Specify the maximum workers in the thread pool. If not specified the number used will be derived from the core count of the environment. This cannot be combined with executor. Default value: None
|
max_lock_renewal_duration
Required
|
A time in seconds that locks registered to this renewer should be maintained for. Default value is 300 (5 minutes). |
on_lock_renew_failure
Required
|
Optional[<xref:LockRenewFailureCallback>]
A callback may be specified to be called when the lock is lost on the renewable that is being registered. Default value is None (no callback). |
executor
Required
|
A user-specified thread pool. This cannot be combined with setting max_workers. |
max_workers
Required
|
Specify the maximum workers in the thread pool. If not specified the number used will be derived from the core count of the environment. This cannot be combined with executor. |
Examples
Automatically renew a message lock
from azure.servicebus import AutoLockRenewer
lock_renewal = AutoLockRenewer(max_workers=4)
with servicebus_receiver:
for message in servicebus_receiver:
# Auto renew message for 1 minute.
lock_renewal.register(servicebus_receiver, message, max_lock_renewal_duration=60)
process_message(message)
servicebus_receiver.complete_message(message)
Automatically renew a session lock
from azure.servicebus import AutoLockRenewer
lock_renewal = AutoLockRenewer(max_workers=4)
with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
session = receiver.session
# Auto renew session lock for 2 minutes
lock_renewal.register(receiver, session, max_lock_renewal_duration=120)
for message in receiver:
process_message(message)
receiver.complete_message(message)
Methods
close |
Cease autorenewal by shutting down the thread pool to clean up any remaining lock renewal threads. |
register |
Register a renewable entity for automatic lock renewal. |
close
Cease autorenewal by shutting down the thread pool to clean up any remaining lock renewal threads.
close(wait: bool = True) -> None
Parameters
Name | Description |
---|---|
wait
|
Whether to block until thread pool has shutdown. Default is True. Default value: True
|
Returns
Type | Description |
---|---|
register
Register a renewable entity for automatic lock renewal.
register(receiver: ServiceBusReceiver, renewable: ServiceBusReceivedMessage | ServiceBusSession, max_lock_renewal_duration: float | None = None, on_lock_renew_failure: LockRenewFailureCallback | None = None) -> None
Parameters
Name | Description |
---|---|
receiver
Required
|
The ServiceBusReceiver instance that is associated with the message or the session to be auto-lock-renewed. |
renewable
Required
|
A locked entity that needs to be renewed. |
max_lock_renewal_duration
|
A time in seconds that the lock should be maintained for. Default value is None. If specified, this value will override the default value specified at the constructor. Default value: None
|
on_lock_renew_failure
|
Optional[<xref:LockRenewFailureCallback>]
A callback may be specified to be called when the lock is lost on the renewable that is being registered. Default value is None (no callback). Default value: None
|
Returns
Type | Description |
---|---|
Azure SDK for Python