Home >Backend Development >Python Tutorial >How to use python thread lock (example analysis)

How to use python thread lock (example analysis)

乌拉乌拉~
乌拉乌拉~Original
2018-08-23 17:42:061811browse

In this article, let’s take a look at what python thread lock is. Learn about python thread locks and what role thread locks can play in python programming.

Thread Lock (Mutex)

Multiple threads can be started under one process, and multiple threads share the memory space of the parent process, which means that each Two threads can access the same data. At this time, if two threads want to modify the same data at the same time, what will happen?

Use of locks:

Create lock
mutex = threading.Lock()

Lock
mutex.acquire([timeout ])

Release
mutex.release()

import time

import threading

def addNum():
    global num #在每个线程中都获取这个全局变量
    print('--get num:',num )
    time.sleep(1)
    num  -=1 #对此公共变量进行-1操作
num = 100  #设定一个共享变量
thread_list = []
for i in range(100):
    t = threading.Thread(target=addNum)
    t.start()
    thread_list.append(t)
for t in thread_list: #等待所有线程执行完毕
    t.join()
    
print('final num:', num )

Normally speaking, the result of this num should be 0, but if you run it a few times on python 2.7, you will find that the final print The num result that comes out is not always 0. Why is the result different every time it is run? Ha, it’s very simple. Suppose you have two threads A and B. At this time, num must be decremented by 1. Since the two threads are running concurrently, it is very likely that the two threads will take away num=100 at the same time. This initial variable is handed over to the CPU for calculation. When thread A completes the calculation, the result is 99, but at this time, thread B completes the calculation and the result is also 99. After the results of the CPU calculations of the two threads at the same time are assigned to the num variable, the results are both 99. then what should we do? It's very simple. When each thread wants to modify public data, in order to prevent others from modifying the data before it has finished modifying it, it can add a lock to the data, so that other threads must wait when they want to modify the data. You can only access this data again after you have completed the modification and released the lock.

Note: Do not run on 3.x. For some reason, the results on 3.x are always correct. It may be that the lock is automatically added.

That’s it. For all the content described in this article, this article mainly introduces the relevant knowledge of python mutex lock. 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 the Python tutorial column on the php Chinese website.

The above is the detailed content of How to use python thread lock (example analysis). 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