Home  >  Article  >  Backend Development  >  A brief introduction to Timer objects, Lock objects and Rlock objects under Python threads

A brief introduction to Timer objects, Lock objects and Rlock objects under Python threads

不言
不言Original
2018-09-11 16:38:341778browse

This article brings you a brief introduction to Timer objects, Lock objects and Rlock objects under Python threads. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Timer object, Lock object, Rlock object

Timer object

Timer object is used to execute a function at a later time.

Syntax:
t=Timer(interval,func,args,kwargs)

Create a timer object and run the function func after interval seconds. args and kwargs provide the parameters and keyword arguments passed to func.
The timer can be started only after calling the start() method.

Commonly used methods:

t.start(): Start the timer.
t.cancal(): If the function has not been executed, cancel the timer.

Lock object

The original lock (mutex lock) is a synchronization primitive and has two states: "Locked" and "Unlocked".

If the state is already locked, attempts to acquire the lock will block until the lock is released. If there are multiple threads waiting to acquire the lock, when the lock is released, only one thread acquires it, and the acquisition order is uncertain.

Syntax:
lock=Lock()

Create a new Lock object, the initial state is unlocked.

Commonly used methods:

lock.acquire(blocking): Acquire the lock. If necessary, block until the lock is released.
If blocking is false, False will be returned immediately when the lock cannot be obtained, and True will be returned if the lock is successfully obtained.

lock.release(): Releases a lock. When the lock is in an unlocked state, or this method is called from a different thread from the thread that originally called the acquire() method, an error will be reported.

Rlock object

A reentrant lock is a synchronization primitive similar to the Lock object, but the same thread can acquire it multiple times.

It allows the thread that owns the lock to perform nested acquire() and release() operations. In this case, only the outermost Release() operation can reset the lock to the slightly unlocked state.

Syntax:
rlock=RLock()

Create a new reentrant lock object.

Commonly used methods:

rlock.acquire(blocking): Acquire the lock. If necessary, block until the lock is released.
If no thread owns the lock, it will be locked, and the recursion level is set to 1.
If this thread already owns the lock, the lock's recursion level is increased by 1, and the function returns immediately.

rlock.release(): Releases the lock by reducing its recursion level. If the recursion level is 0 after decrement, the lock will be reset to the unlocked state. Otherwise, the lock will remain locked.
This method can only be called by the thread that currently owns the lock.

Related recommendations:

Class and object learning tutorial in Python object-oriented programming

Python multi-threading Synchronize Lock, RLock, Semaphore, and Event instances

The above is the detailed content of A brief introduction to Timer objects, Lock objects and Rlock objects under Python threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn