Python often uses multiple threads, which means that all methods start at the same time, rather than sequentially
run. The module used is threading, and the following is a detailed threading usage.
There's only one () method,two,three, and the same content.
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/9D/6B/wKiom1l_9KWTCtTgAAAp1kCJJF8990.png-wh_500x0-wm_ 3-wmp_4-s_4059096748.png "Title=" write three methods. png "alt=" wkiom1l_9kwtcttgaaap1kcjjf8990.png-wh_50 "/>
As shown in the figure below, three functions are run at different times, respectively.
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/9D/6B/wKiom1l_9LKz7NtCAABtOuTkVXM865.png-wh_500x0-wm_ 3-wmp_4-s_2315058694.png "Title=" runs three methods at different times. png "alt=" wkiom1l_9lkz7ntcaabtoutkvxm865.png-wh_50 "/>
Define a thread pool and write the threads that you want to run to this thread pool list:
Threads= [] #define a pool of threads
T1= Threading. Thread (Target= One,args=(,)) #Create a thread and assign it toT1, this thread specifies the calling method One, with no parameters
Threads. Append (t1)#putT1thread loaded intoThreadsLine Constructor
T2= Threading. Thread (Target=Both )
threads. Append (T2)
T3= Threading. Thread (Target=three)
threads. Append (T3)
At this point threads has three threads in the list.
The following is the thread that runs inside this thread pool
forTinchThreads:
T.setdaemon (True)#StatementTfor the daemon thread, the child thread will run with the main thread and end directly, no more sub-threads inside the loop will be executed.
T.start ()
T.join ()#function is to execute all child threads before executing the main thread
use a for statement to traverse the thread in the threads, and then call the start () method to run
Note that t.join () must be placed outside the for statement.
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/9D/6B/wKioL1l_9L2SOoqzAAArsXnj3cQ747.png-wh_500x0-wm_ 3-wmp_4-s_3111532750.png "title=" run result. png "alt=" wkiol1l_9l2sooqzaaarsxnj3cq747.png-wh_50 "/>
is it quick to understand the use of threading?
there are problems to communicate QQ Group: 610845268
This article is from the "It Bug" blog, so be sure to keep this source http://laomomo.blog.51cto.com/6595318/1952603
Python Multithreading threading Usage