程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Multithreaded Python implementation and multithreaded ordering

編輯:Python

List of articles

  • Preface
  • One 、 Multithreading runs out of order
  • Two 、“join Method ” Solve the problem of disordered multithreading
  • 3、 ... and 、threading.Thread() Common parameters of
  • summary


Preface

Multithreading is generally used to call multiple functions at the same time ,cpu Time slices are assigned to multiple tasks in turn . The advantage is to improve cpu The usage rate of , Make the computer reduce the total time of processing multiple tasks ; The disadvantage is that if there are global variables , Calling multiple functions will cause the global variables to be modified by multiple functions , Cause calculation error , This makes it necessary to use join Method or set local variables to solve the problem .python Use threading Module to achieve multithreading ,threading.join() The method is to ensure that join When the child thread of is completed , To distribute cpu To other sub threads , So as to ensure the orderly operation of threads .


One 、 Multithreading runs out of order

Let's first create three instances ,t1,t2,t3 t1 The instance function1 function ,t2 and t3 Function call function11 function , They are all global variables l1 To operate
The code is as follows ,

import threading,time
l1 = []
# establish RLock lock ,acquire A few times ,release A few times 
lock = threading.RLock()
def function1(x,y):
for i in range(x):
l1.append(i)
if i == 0:
time.sleep(1)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1 ))
def function11(x,y):
for i in range(x):
l1.append(i)
end_time = time.time()
print("t{} is finished in {}s".format(y, end_time -time1))
#2. Create child threads :thread class 
if __name__ == '__main__':
t1 = threading.Thread(target= function1, args = (100,1))
t2 = threading.Thread(target= function11, args = (100,2))
t3 = threading.Thread(target= function11, args = (100,3))
time1 = time.time()
print("time starts in {}".format(time1))
t1.start()
t2.start()
t3.start()
print(l1)

give the result as follows ,

runfile('E:/ desktop /temp.py', wdir='E:/ desktop ')
time starts in 1656474963.9487
t2 is finished in 0.0s
t3 is finished in 0.0s
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
t1 is finished in 1.0152690410614014s

We can see , There are two at the beginning of the global variable 0, Instead of pressing 0,1,2,3 In order to fill , So we can know that global variables are controlled by multiple functions in multithreading disorder Called . To ensure that multiple threads call global variables in order , We can use threading.join() Methods .

Two 、“join Method ” Solve the problem of disordered multithreading

We rewrote function1 function , And named it function2,t1 call function2 function .t2,t3 unchanged .
The code is as follows ,

import threading,time
l1 = []
# establish RLock lock ,acquire A few times ,release A few times 
lock = threading.RLock()
def function1(x,y):
for i in range(x):
l1.append(i)
if i == 0:
time.sleep(1)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1))
def function11(x,y):
for i in range(x):
l1.append(i)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1))
def function2(x,y):
for i in range(x):
l1.append(i)
if i == 0:
time.sleep(1)
end_time = time.time()
print("t{} is finished in {}s".format(y,end_time -time1))
#2. Create child threads :thread class 
if __name__ == '__main__':
t1 = threading.Thread(target= function2, args = (100,1))
t2 = threading.Thread(target= function11, args = (100,2))
t3 = threading.Thread(target= function11, args = (100,3))
time1 = time.time()
print("time starts in {}".format(time1))
t1.start()
t1.join()
t2.start()
t3.start()
print(l1)

give the result as follows ,

runfile('E:/ desktop /temp.py', wdir='E:/ desktop ')
time starts in 1656476057.441827
t1 is finished in 1.0155227184295654s
t2 is finished in 1.0155227184295654s
t3 is finished in 1.0155227184295654s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

thus it can be seen ,threading.join() Method can solve multithreading disorder problem

3、 ... and 、threading.Thread() Common parameters of

1.group: The default value is None, In order to achieve ThreadGroup Class while retaining
2.target: stay start Callable object invoked in the method , That is, the callable object that needs to open the thread , Like functions 、 Method
3.name: The default is “Thread-N”, Thread name in string form
4.args: The default is empty tuple , Parameters target The parameter tuple of the callable object passed in
5.kwargs: The default is empty dictionary {}, Parameters target The keyword parameter Dictionary of the callable object passed in
6.daemon: The default is None

summary

That's all python Introduction to common parameters of multithreading and multithread ordering , There will be more in-depth multi-threaded articles in the future. Please continue to pay attention to .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved