API Locking¶
The interfaces in this chapter are used to lock all API interfaces, allowing you to lock the interfaces for exclusive use by the current Device instance and preventing the API from being used by other users or processes. You can set a default lock duration or refresh the lock periodically yourself. The recommended approach is to refresh the lock periodically. You can also release the API lock actively. A robust locking process is: acquire lock -> create a thread to periodically refresh the lock -> release lock.
Acquire Lock¶
Acquires a lock. This lock will be automatically released after 60 seconds. After it is automatically released, other clients will be able to acquire the lock. You can change this duration, but if it is set too high and the script exits abnormally, you may be almost permanently unable to connect to the device. You might need to restart the device to connect again. This interface is re-entrant; re-entering is equivalent to calling _refresh_lock.
d._acquire_lock(leaseTime=60)
Release Lock¶
Actively releases the API lock. Afterward, other clients will be able to acquire the lock.
d._release_lock()
Refresh Lock¶
Refreshes the lock. Each call sets the lock's expiration time to the specified leaseTime. Call this interface periodically to maintain the API lock. Similarly, if leaseTime is set too high and the script exits abnormally, you may be almost permanently unable to connect to the device.
d._refresh_lock(leaseTime=60)