Interface Locking

The interfaces described in this chapter are used to lock all API interfaces, allowing you to restrict access to the current Device instance and prevent other users or processes from using the API. You can set a default lock duration or periodically refresh the lock manually—periodic refreshing is recommended. You may also explicitly release the interface lock. A safe locking procedure involves: acquiring the lock → creating a thread to periodically refresh the lock → releasing the lock.

Acquire Lock

Acquires the lock. The lock will be automatically released after 60 seconds, after which other clients can acquire it. You can change this time, but setting it too high may result in being unable to connect to the device if the script exits unexpectedly—you might need to restart the device to regain access. This interface is reentrant; reentering is equivalent to calling _refresh_lock.

d._acquire_lock(leaseTime=60)

Release Lock

Releases the API lock explicitly, allowing other clients to acquire the lock afterward.

d._release_lock()

Refresh Lock

Refreshes the lock, resetting its expiration time to the specified leaseTime upon each call. Call this interface periodically to maintain the API lock. Similarly, if leaseTime is set too high, an unexpected script exit may leave you nearly permanently unable to reconnect to the device.

d._refresh_lock(leaseTime=60)