Explain the list under Python multithreading in detail

List is one of several basic data types commonly used in Python. Under normal circumstances, we will add, delete, modify and check the list. Obviously, there will be no problems if we try to operate the list in multiple threads. ?

List under multithreading

**Safe or unsafe? Not safe! **

Generally speaking, thread safety means that all operations on a certain data structure are thread-safe. Under this definition, Python’s commonly used data structures list, dict, str, etc. are all thread-unsafe

Although list under multithreading is thread-unsafe, it is thread-safe under the operation of append.

**How to judge thread safety? **

As for thread safety and unsafety, we can reproduce it through extreme conditions and draw conclusions. For example, judge whether list is thread safe

import threading
import time

# Set the value of count at will, the larger the value, the faster the error will be thrown
count =1000
l =[]

def add():for i inrange(count):
 l.append(i)
 time.sleep(0.0001)

def remove():for i inrange(count):
 l.remove(i)
 time.sleep(0.0001)

t1 = threading.Thread(target=add)
t2 = threading.Thread(target=remove)
t1.start()
t2.start()
t1.join()
t2.join()print(l)

Sometimes an error is not necessarily caused by one run, and an error similar to the following will appear after multiple retries

Obviously, this method of operation is not universal. If the European spirit is too strong, there may be no abnormalities.

So out of this way, is there a simple and effective method? The answer is yes

dis

The dis library is a library that comes with Python and can be used to analyze bytecode. Here we need to have the understanding that each line of bytecode is an atomic operation, and multi-thread switching is based on atomic operations. If an operation requires two lines of bytecode, it means that it is thread-unsafe.

remove

Here we first look at the remove operation of the above list

import dis
 def test_remove():...   a =[1]...   a.remove(0)... 
 dis.dis(test_remove)20 LOAD_CONST        1(1)2 BUILD_LIST        14 STORE_FAST        0(a)36 LOAD_FAST        0(a)8 LOAD_ATTR        0(remove)10 LOAD_CONST        2(0)12 CALL_FUNCTION      114 POP_TOP
  16 LOAD_CONST        0(None)18 RETURN_VALUE

It is not difficult to see from the above that the entire remove operation is divided into several instructions, which means that there will be confusion in the case of multi-threading. Imagine if you go to the remove list under multi-threading, and not In order, problems are prone to occur.

append

At the top we said that the append operation of list is thread-safe, so why exactly? Let's also use dis to check

819 LOAD_GLOBAL       0(a)22 LOAD_ATTR        2(append)25 LOAD_CONST        2(1)28 CALL_FUNCTION      131 POP_TOP  

Obviously, there are several instructions in append, which will inevitably be interleaved in the case of multi-threaded execution, but for the operation of append under multi-threading, we definitely don’t care about the order of list at this time. , So we say that its append is thread-safe

reference

https://stackoverflow.com/questions/6319207/are-lists-thread-safe/19728536#19728536

https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe

The above is the detailed content of the list under Python multithreading. For more information about the list under Python multithreading, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

Explain the list under Python multithreading in detail
Python randomly shuffles the elements in the list
The usage of wheel in python
What is list comprehension in python
​What are the numbers in Python?
Talking about the modules in Python
The usage of tuples in python
Understanding the meaning of rb in python
The usage of Ajax in Python3 crawler
Python multithreading
Python3 list
What is the function of adb in python
How to use the round function in python
How to use the zip function in Python
An article to understand the yield in Python
What does the tab key in python mean
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
How to use the format function in python
First acquainted with python, the grammatical rules in python
The consequences of uninstalling python in ubuntu, very
How to install the downloaded module in python
Mount the disk in a directory under Ubuntu 18.04
The best way to judge the type in Python
Functions in python
python list learning
2.1 The Python Interpreter (python interpreter)
[902] python list sort
Installation and use of GDAL in Python under Ubuntu
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Python data visualization: who are the big names in Python?
For the first time in history, Python surpassed Java!
What are the ways to open files in python
Resolve the problems encountered in the linux environment under ubuntu