Python multi-thread synchronization Lock, RLock, Semaphore, Event instance

Source: Internet
Author: User
This article mainly introduces Python multi-thread synchronization Lock, RLock, Semaphore, Event instances, and Lock & amp; RLock to ensure multi-thread multi-shared resource access, semaphore is used to ensure the upper limit of multi-thread access to certain resources. Event is the simplest way of inter-thread communication. For more information, see I. multi-thread synchronization

Because the python interpreter of CPython is executed in single-threaded mode, many threads of python cannot fully utilize the resources of multi-core CPUs. Multi-process is recommended in most cases.

The synchronization of multiple threads in python is basically the same as that in other languages, mainly including:

Lock & RLock: used to ensure multi-threaded multi-shared resource access.
Semaphore: used to ensure the upper limit of multi-thread access to certain resources, such as resource pools.
Event: The simplest way to communicate between threads. one thread can send signals, and other threads can perform operations after receiving signals.

II. instances

1) Lock & RLock

The status of the Lock object can be locked or unlocked.

Use acquire () to set the status to locked;
Use release () to set the status to unlocked.

If the current status is unlocked, acquire () changes the status to locked and returns immediately. When the status is locked, acquire () will be blocked until another thread Calls release () to change the status to unlocked, and then acquire () can set the status to locked again.

Lock. acquire (blocking = True, timeout =-1). the blocking parameter indicates whether to block the waiting time of the current thread, and the timeout parameter indicates the waiting time during blocking. If the lock is obtained successfully, the acquire () function returns True; otherwise, False is returned. if the lock is not obtained during timeout, False is returned.

Instance: (ensure that only one thread can access shared resources)

The code is as follows:


Import threading
Import time

Num = 0
Lock = threading. Lock ()

Def func (st ):
Global num
Print (threading. currentThread (). getName () + 'try to acquire the Lock ')
If lock. acquire ():
Print (threading. currentThread (). getName () + 'acquire the lock .')
Print (threading. currentThread (). getName () + ": % s" % str (num ))
Num + = 1
Time. sleep (st)
Print (threading. currentThread (). getName () + 'release the lock .')
Lock. release ()

T1 = threading. Thread (target = func, args = (8 ,))
T2 = threading. Thread (target = func, args = (4 ,))
T3 = threading. Thread (target = func, args = (2 ,))
T1.start ()
T2.start ()
T3.start ()

Result:

The difference between RLock and Lock is that in addition to the locked and unlocked statuses, RLock also records the owner and number of recursive layers of the current lock, so that RLock can be acquire () multiple times by the same thread ().

2) Semaphore

Semaphore manages a built-in counter,
Built-in counter-1 every time acquire () is called;
Built-in counter + 1 when calling release;
The counter cannot be smaller than 0. when the counter is 0, acquire () will block the thread until other threads call release ().

Instance: (only two threads can obtain semaphore at the same time, that is, the maximum number of connections can be limited to 2 ):

The code is as follows:


Import threading
Import time

Semaphore = threading. Semaphore (2)

Def func ():
If semaphore. acquire ():
For I in range (5 ):
Print (threading. currentThread (). getName () + 'get semaphore ')
Semaphore. release ()
Print (threading. currentThread (). getName () + 'release semaphore ')


For I in range (4 ):
T1 = threading. Thread (target = func)
T1.start ()

Result:

3) Event

The Event contains a flag, which is false at the beginning.
You can use set () to set it to true;
Or use clear () to reset it to false;
You can use is_set () to check the Flag status;
The other most important function is wait (timeout = None), which is used to block the current thread until the internal flag of the event is set to true or timeout. If the internal flag is true, the wait () function is returned.

Instance: (threads communicate with each other)

The code is as follows:


Import logging
Import threading
Import time

Logging. basicConfig (level = logging. DEBUG,
Format = "(% (threadName)-10 s: % (message) s ",
)

Def wait_for_event_timeout (e, t ):
"Wait t seconds and then timeout """
While not e. isSet ():
Logging. debug ("wait_for_event_timeout starting ")
Event_is_set = e. wait (t)
Logging. debug ("event set: % s" % event_is_set)
If event_is_set:
Logging. debug ("processing event ")
Else:
Logging. debug ("doing other work ")

E = threading. Event ()
T2 = threading. Thread (name = "nonblock ",
Target = wait_for_event_timeout, args = (e, 2 ))
T2.start ()
Logging. debug ("Waiting before calling Event. set ()")
Time. sleep (7)
E. set ()
Logging. debug ("Event is set ")

Running result:

III. others

1) thread local variables

The value of a local variable in a thread is related to the thread. The difference is different from that of a global variable. The usage is very simple as follows:

The code is as follows:


Mydata = threading. local ()
Mydata. x = 1

2) use the with keyword for Lock, semaphore, and condition to call acquire () and release () manually ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.