What is python multi-thread synchronization? What is a thread lock?

乌拉乌拉~
Release: 2018-08-21 19:44:37
Original
1903 people have browsed it

For those who are exposed to the python programming language for the first time, they know little about python thread synchronization when they first started learning python programming. In this article, we will learn about itpythonmultithreadingSynchronization andpythonmultithreading lockknow this aspect.

Thread synchronization

If multiple threads jointly modify a certain data, unpredictable results may occur. In order to ensure the correctness of the data, multiple threads need to be modified. Threads are synchronized.

Simple thread synchronization can be achieved using the Thread object's Lock and Rlock. Both objects have acquire methods and release methods. For data that requires only one thread to operate at a time, its operations can be placed Between acquire and release methods. As follows:

The advantage of multi-threading is that it can run multiple tasks at the same time (at least it feels like this). But when threads need to share data, there may be data out-of-synchronization problems.

Consider this situation: all elements in a list are 0, thread "set" changes all elements to 1 from back to front, and thread "print" is responsible for reading the list from front to back and printing .

Then, maybe when the thread "set" starts to change, the thread "print" will print the list, and the output will be half 0 and half 1. This is the desynchronization of the data. To avoid this situation, the concept of locks was introduced.

Multi-threaded lock

The lock has two states - locked and unlocked. Whenever a thread such as "set" wants to access shared data, it must first obtain the lock; if another thread such as "print" has obtained the lock, then let the thread "set" pause, which is synchronous blocking; wait until the thread " Print "After the access is completed and the lock is released, let the thread "set" continue.

After such processing, when printing the list, either all 0s or all 1s will be output, and there will no longer be an embarrassing scene of half 0s and half 1s.

Example

An example is as follows:

#!/usr/bin/python # -*- coding: UTF-8 -*- import threading import time class myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print "Starting " + self.name # 获得锁,成功获得锁定后返回True # 可选的timeout参数不填时将一直阻塞直到获得锁定 # 否则超时后将返回False threadLock.acquire() print_time(self.name, self.counter, 3) # 释放锁 threadLock.release() def print_time(threadName, delay, counter): while counter: time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 threadLock = threading.Lock() threads = [] # 创建新线程 thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # 开启新线程 thread1.start() thread2.start() # 添加线程到线程列表 threads.append(thread1) threads.append(thread2) # 等待所有线程完成 for t in threads: t.join() print "Exiting Main Thread"
Copy after login

The above is all the content described in this article, this article mainly introducesPython multi-thread synchronizationrelated knowledge, I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit thePython tutorialcolumn on the php Chinese website.

The above is the detailed content of What is python multi-thread synchronization? What is a thread lock?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!