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