Python multithreading Threading beginner's tutorial, pythonthreading
1.1 What is multithreading Threading
Multithreading can be simply understood as executing multiple tasks at the same time.
Multiple processes and threads can execute multiple tasks, and threads are part of the process. The characteristics of threads are that they can share memory and variables with each other and consume less resources (however, in Unix environments, the gap between multi-process and multi-thread resource scheduling consumption is not obvious, and Unix scheduling is faster ), the disadvantage is that it is troublesome to synchronize and lock between threads.
1.2 add Thread
Import Module
import threading
Obtain the number of activated threads
threading.active_count()
View All thread Information
threading.enumerate()
View the currently running thread
threading.current_thread()
Add thread,threading.Thread()The receiving parameter target indicates the task to be completed by this thread and needs to be defined by itself.
Def thread_job (): print ('this is a thread of % s' % threading. current_thread () def main (): thread = threading. thread (target = thread_job,) # define thread Thread. start () # Let the thread start working if _ name _ = '_ main _': main ()
1.3 join
Because the thread is executed at the same time, the join function allows the thread to complete the next operation, that is, to block the calling thread until all tasks in the queue are processed.
import threadingimport timedef thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finish\n')def T2_job(): print('T2 start\n') print('T2 finish\n')def main(): added_thread=threading.Thread(target=thread_job,name='T1') thread2=threading.Thread(target=T2_job,name='T2') added_thread.start() #added_thread.join() thread2.start() #thread2.join() print('all done\n')if __name__=='__main__': main()
As shown in the preceding example, if the join function is not used, the result is as follows:
After the join function is executed, T2. after T1 is run, print ('all one') is run ')
1.4 save the process result queue
The queue is a thread-safe queue (FIFO) implementation in the python standard library. It provides a first-in-first-out data structure suitable for multi-thread programming, that is, the queue, used to transmit information between producer and consumer threads
(1) Basic FIFO queue
class queue.Queue(maxsize=0)
Maxsize is an integer that indicates the maximum number of data stored in the queue. When the upper limit is reached, insertion will lead to blocking until the data in the queue is consumed. If maxsize is less than or equal to 0, no queue size limit
(2) last in first out, first in, first out
class queue.LifoQueue(maxsize=0)
(3) priority queue
class queue.PriorityQueue(maxsize=0)
The code in the video is not quite clear.
import threadingimport timefrom queue import Queuedef job(l,q): for i in range(len(l)): l[i]=l[i]**2 q.put(l)def multithreading(): q=Queue() threads=[] data=[[1,2,3],[3,4,5],[4,5,6],[5,6,7]] for i in range(4): t=threading.Thread(target=job,args=(data[i],q)) t.start() threads.append(t) for thread in threads: thread.join() results=[] for _ in range(4): results.append(q.get()) print(results)if __name__=='__main__': multithreading()
The running result is as follows:
Image Capture Source: http://www.cnblogs.com/itogo/p/5635629.html
1.5 GIL is not necessarily efficient
Global Interpreter Lock: The Global Interpreter Lock. python execution is controlled by the python Virtual Machine (also called the main loop of the Interpreter), and GIL controls access to the python virtual machine to ensure the access at any time, only one thread runs in the interpreter. In a multi-threaded environment, the python virtual machine is executed as follows:
1. Set GIL
2. Switch to a thread to run
3. Run:
A. specify the number of bytecode commands, or
B. The thread actively controls outbound traffic (time. sleep (0) can be called ))
4. Set the thread to sleep
5. Unlock GIL
6. Repeat 1-5
GIL will be locked when calling external code (such as C/C ++ extensions, until the end of this function (because no python bytecode is run during this period, thread switching is not performed ).
The following code is used as an example in the video. It expands the number by 4 times and is divided into the normal mode and is allocated to four threads. It is found that the time consumption is not significantly different.
import threadingfrom queue import Queueimport copyimport timedef job(l, q): res = sum(l) q.put(res)def multithreading(l): q = Queue() threads = [] for i in range(4): t = threading.Thread(target=job, args=(copy.copy(l), q), name='T%i' % i) t.start() threads.append(t) [t.join() for t in threads] total = 0 for _ in range(4): total += q.get() print(total)def normal(l): total = sum(l) print(total)if __name__ == '__main__': l = list(range(1000000)) s_t = time.time() normal(l*4) print('normal: ',time.time()-s_t) s_t = time.time() multithreading(l) print('multithreading: ', time.time()-s_t)
The running result is:
1.6 thread Lock
If thread 1 gets the result and you want thread 2 to continue to use the result of 1 for processing, You need to lock thread 1 and wait until thread 2 is finished. Generally, lock is used for processing shared memory.
Import threadingdef job1 (): global A, lock # global variable lock. acquire () # Start lock for I in range (10): A + = 1 print ('job1', A) lock. release () # release def job2 (): global A, lock. acquire () for I in range (10): A + = 10 print ('job2', A) lock. release () if _ name _ = '_ main _': lock = threading. lock () A = 0 t1 = threading. thread (target = job1) t2 = threading. thread (target = job2) t1.start () t2.start () t1.join () t2.join ()
The running result is as follows:
Summary
The above is a Python multi-thread Threading beginner tutorial. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!