For Python's multi-threading understanding, I spent a lot of time searching for most of the articles that were not easy to understand. So, here is trying to use a simple example, so you have a preliminary understanding of multithreading.
Single Thread
Many years ago in MS-DOS ERA, operating system processing problems are single-tasking, I want to do listening to music and watching movies two things, then be sure to arrange the order first.
(All right!) We don't struggle with the use of listening to music and seeing shadows in the age of DOS. ^_^)
The code is as follows:
From time import Ctime,sleep
def music ():
For I in range (2):
Print "I was listening to music. %s "%ctime ()
Sleep (1)
def move ():
For I in range (2):
Print "I am at the movies! %s "%ctime ()
Sleep (5)
if __name__ = = ' __main__ ':
Music ()
Move ()
Print "All over%s"%ctime ()
We listened to a piece of music and controlled the music playback two times with a For loop, which took 1 seconds and sleep () to control how long the music was playing. Then we saw another movie,
Every movie takes 5 seconds because it looks so good, so I look at it two times through the for loop. At the end of the entire leisure activity, I passed
The code is as follows:
Print "All over%s"%ctime ()
Look at the current time, it's almost bedtime.
Operation Result:
The code is as follows:
>>=========================== RESTART ================================
>>>
I was listening to music. Thu APR 17 10:47:08 2014
I was listening to music. Thu APR 17 10:47:09 2014
I was at the movies! Thu APR 17 10:47:10 2014
I was at the movies! Thu APR 17 10:47:15 2014
All over Thu APR 17 10:47:20 2014
In fact, music () and move () should be seen as a musical and video player, as to what songs and videos to play should be determined by our use. So, we have modified the above code:
The code is as follows:
#coding =utf-8
Import threading
From time import Ctime,sleep
def music (func):
For I in range (2):
Print "I was listening to%s.%s"% (Func,ctime ())
Sleep (1)
def move (func):
For I in range (2):
Print "I am at the%s! %s "% (Func,ctime ())
Sleep (5)
if __name__ = = ' __main__ ':
Music (U ' Love Trade ')
Move (U ' Avatar ')
Print "All over%s"%ctime ()
The music () and move () are passed to the parameter. Experience Chinese classic songs and large-and-Western culture.
Operation Result:
The code is as follows:
>>> ======================== RESTART ================================
>>>
I was listening to love business. Thu APR 17 11:48:59 2014
I was listening to love business. Thu APR 17 11:49:00 2014
I was at the avatar! Thu APR 17 11:49:01 2014
I was at the avatar! Thu APR 17 11:49:06 2014
All over Thu APR 17 11:49:11 2014
Multithreading
Science and technology in the development, the times in progress, our CPU is also getting faster, CPU complaining, p big things accounted for me a certain time, in fact, I also do a lot of work is no problem;
into a multi-tasking era. We listen to the music eating hot pot is not the dream.
Python provides two modules to implement multithreaded thread and threading, thread has some drawbacks, in threading get made up, in order not to waste you and time, so we straight
Pick up the learning threading and you can.
Continue to transform the example above, introducing threadring to play music and video simultaneously:
The code is as follows:
#coding =utf-8
Import threading
From time import Ctime,sleep
def music (func):
For I in range (2):
Print "I was listening to%s.%s"% (Func,ctime ())
Sleep (1)
def move (func):
For I in range (2):
Print "I am at the%s! %s "% (Func,ctime ())
Sleep (5)
Threads = []
T1 = Threading. Thread (target=music,args= (U ' Love Trade ',))
Threads.append (T1)
T2 = Threading. Thread (target=move,args= (U ' Avatar ',))
Threads.append (T2)
if __name__ = = ' __main__ ':
For T in Threads:
T.setdaemon (True)
T.start ()
Print "All over%s"%ctime ()
Import threading
First, import the threading module, which is the premise of using multithreading.
The code is as follows:
Threads = []
T1 = Threading. Thread (target=music,args= (U ' Love Trade ',))
Threads.append (T1)
Creates a threads array, creates thread T1, and uses threading. The Thread () method, in which the music method is called by the Target=music,args method to pass a parameter to the music. To create
The built thread T1 into the threads array.
The thread T2 is then created in the same way, and the T2 is also loaded into the threads array.
The code is as follows:
For T in Threads:
T.setdaemon (True)
T.start ()
Finally, the array is traversed by a for loop. (the array is loaded with T1 and T2 two threads)
Setdaemon ()
Setdaemon (True) declares a thread as a daemon thread and must be set before the start () method call, if not set to the daemon, is indefinitely suspended. When a child thread starts, the parent line
Process continues, when the parent thread finishes executing the last statement print "All over%s"%ctime (), does not wait for the child thread, exits directly, and the child threads end together.
Start ()
Starts the thread activity.
Operation Result:
The code is as follows:
>>> ========================= RESTART ================================
>>>
I was listening to love business. Thu APR 12:51:45 I was at the avatar! Thu Apr 12:51:45 all over Thu Apr 17 12:51:45 2014
From the execution result, the child thread (Muisc, move) and the main thread (print "All over%s"%ctime ()) are started at the same time, but cause the child thread to terminate because the main thread finishes executing.
Continue to adjust the program:
The code is as follows:
...
if __name__ = = ' __main__ ':
For T in Threads:
T.setdaemon (True)
T.start ()
T.join ()
Print "All over%s"%ctime ()
We only added a join () method to the above program to wait for the thread to terminate. The role of Join () is that the parent thread of this child thread will remain blocked until the child thread finishes running.
Note: The position of the join () method is outside the for loop, which means that you must wait for the two processes in the For loop to finish before executing the main process.
Operation Result:
The code is as follows:
>>> ========================= RESTART ================================
>>>
I was listening to love business. Thu APR 13:04:11 I was at the avatar! Thu APR 17 13:04:11 2014
I was listening to love business. Thu APR 17 13:04:12 2014
I was at the avatar! Thu APR 17 13:04:16 2014
All over Thu APR 17 13:04:21 2014
As you can see from the execution results, music and move are started at the same time.
The start time is 4 minutes and 11 seconds, until the main process is called 4 minutes and 22 seconds, which takes a total of 10 seconds. With a reduction of 2 seconds from a single thread, we can adjust the time of sleep () of music to 4 seconds.
The code is as follows:
...
def music (func):
For I in range (2):
Print "I was listening to%s.%s"% (Func,ctime ())
Sleep (4)
...
Execution Result:
The code is as follows:
>>> ====================== RESTART ================================
>>>
I was listening to love business. Thu APR 13:11:27 2014I was at the avatar! Thu APR 17 13:11:27 2014
I was listening to love business. Thu APR 17 13:11:31 2014
I was at the avatar! Thu APR 17 13:11:32 2014
All over Thu APR 17 13:11:37 2014
The child thread starts 11 minutes and 27 seconds, and the main thread runs 11 minutes and 37 seconds.
Although the music each song from 1 seconds extended to 4, but the way to run a multi-thread script, the total time has not changed.
This article from the perceptual let you quickly understand the Python multi-threaded use, more detailed use please refer to other documents or materials.
==========================================================
Class Threading. Thread () Description:
The code is as follows:
Class Threading. Thread (Group=none, Target=none, Name=none, args= (), kwargs={})
This constructor should is called with keyword arguments. Arguments is:
Group should be None; Reserved for future extension if a Threadgroup class is implemented.
Target is the callable object to being invoked by the run () method. Defaults to None, meaning nothing is called.
Name is the thread name. By default, a unique name was constructed of the form "Thread-n" where N is a small decimal number.
Args is the argument tuple for the target invocation. Defaults to ().
Kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__ ()) befo Re doing
Anything else to the thread.
Before talking about a multi-threaded blog, I feel the meaning of the words, in fact, multithreading is very interesting. Because we are in the process of using the computer in many processes and multi-threading. We can continue with the previous example. Please read my previous blog first.
The creation of threads from the above example is rather cumbersome, and each thread created needs to create a TX (T1, T2 、... ), if you create a thread that is extremely inconvenient. The following examples continue to improve:
player.py
The code is as follows:
#coding =utf-8
From time import sleep, CTime
Import threading
def muisc (func):
For I in range (2):
print ' Start Playing:%s! %s '% (Func,ctime ())
Sleep (2)
def move (func):
For I in range (2):
print ' Start Playing:%s! %s '% (Func,ctime ())
Sleep (5)
def player (name):
r = Name.split ('. ') [1]
if r = = ' mp3 ':
Muisc (name)
Else
if r = = ' mp4 ':
Move (name)
Else
print ' error:the format is not recognized! '
list = [' Love trading. mp3 ', ' Avatar. mp4 ']
Threads = []
Files = range (len (list))
#创建线程
For I in Files:
t = Threading. Thread (target=player,args= (List[i],))
Threads.append (t)
if __name__ = = ' __main__ ':
#启动线程
For I in Files:
Threads[i].start ()
For I in Files:
Threads[i].join ()
#主线程
print ' end:%s '%ctime ()
Interestingly, we also created a player () function, which is used to determine the type of playback file. If it is in MP3 format, we will call the music () function, if it is the MP4 format we call the Move () function. Which two formats are not so only to tell the user that you have provided a file I can not play.
Then we create a list of file lists, and note that the file is prefixed with a suffix name. We then use Len (list) to calculate how many files the list has, which is to help us determine the number of loops.
We then add the file in the list to the thread array threads[] in a for loop. Then start the threads[] thread group, and the last print end time.
Split () splits a string into two parts and then takes part of it.
The code is as follows:
>>> x = ' testing.py '
>>> s = X.split ('. ') [1]
>>> if s== ' py ':
Print S
Py
Operation Result:
The code is as follows:
Start Playing: Love business. mp3! Mon APR 21 12:48:40 2014
Start Playing: Avatar. mp4! Mon APR 21 12:48:40 2014
Start Playing: Love business. mp3! Mon APR 21 12:48:42 2014
Start Playing: Avatar. mp4! Mon APR 21 12:48:45 2014
End:mon APR 21 12:48:50 2014
Now add a file to the list array, and the program will automatically create a thread for it when it runs.
Continue to improve the example:
Through the program above, we find that the player () is used to determine the file extension, then Call Music () and move (), in fact, music () and move () The full work is the same, why we do not do a super player, no matter what the file can be played. After the makeover, my super player was born.
super_player.py
The code is as follows:
#coding =utf-8
From time import sleep, CTime
Import threading
def super_player (file,time):
For I in range (2):
print ' Start Playing:%s! %s '% (File,ctime ())
Sleep (Time)
#播放的文件与播放时长
List = {' Love trading. mp3 ': 3, ' Avatar mp4 ': 5, ' Me and you. mp3 ': 4}
Threads = []
Files = range (len (list))
#创建线程
For File,time in List.items ():
t = Threading. Thread (target=super_player,args= (file,time))
Threads.append (t)
if __name__ = = ' __main__ ':
#启动线程
For I in Files:
Threads[i].start ()
For I in Files:
Threads[i].join ()
#主线程
print ' end:%s '%ctime ()
The dictionary list is created first to define the file to be played in time (seconds), and the dictionary's items () method is used to iterate through the files and hours, and the two values are taken to create the thread.
The Super_player () function is then created to receive file and time to determine the length of the file to be played.
Finally, the thread starts running. Operation Result:
The code is as follows:
Start Playing: Love business. mp3! Fri APR 25 09:45:09 2014
Start Playing: Me and you, mp3!. Fri APR 25 09:45:09 2014
Start Playing: Avatar. mp4! Fri APR 25 09:45:09 2014
Start Playing: Love business. mp3! Fri APR 25 09:45:12 2014
Start Playing: Me and you, mp3!. Fri APR 25 09:45:13 2014
Start Playing: Avatar. mp4! Fri APR 25 09:45:14 2014
END:FRI APR 25 09:45:19 2014
To create your own multithreaded class
The code is as follows:
#coding =utf-8
Import threading
From time import sleep, CTime
Class MyThread (threading. Thread):
def __init__ (self,func,args,name= "):
Threading. Thread.__init__ (self)
Self.name=name
Self.func=func
Self.args=args
def run (self):
Apply (Self.func,self.args)
def super_play (file,time):
For I in range (2):
print ' Start Playing:%s! %s '% (File,ctime ())
Sleep (Time)
List = {' Love trading. mp3 ': 3, ' Avatar. mp4 ': 5}
#创建线程
Threads = []
Files = range (len (list))
For k,v in List.items ():
t = MyThread (Super_play, (k,v), super_play.__name__)
Threads.append (t)
if __name__ = = ' __main__ ':
#启动线程
For I in Files:
Threads[i].start ()
For I in Files:
Threads[i].join ()
#主线程
print ' end:%s '%ctime ()
MyThread (Threading. Thread)
Creates a Mythread class for inheriting threading. Thread class.
__init__ ()
Initializes parameters such as Func, args, name, and so on, using the class's initialization method.
Apply ()
The Apply (func [, args [, Kwargs]]) function is used to indirectly invoke a function when a function parameter already exists in a tuple or dictionary. Args is a tuple that contains the arguments passed by position that will be supplied to the function. If args is omitted, no arguments are passed, and Kwargs is a dictionary containing the keyword arguments.
Apply () Usage:
The code is as follows:
#不带参数的方法
>>> def say ():
print ' Say in '
>>> Apply (say)
Say in
#函数只带元组的参数
>>> def say (A, B):
Print A, b
>>> apply (say, (' hello ', ' Bug Master ')
Hello, insect Master.
#函数带关键字参数
>>> def Say (a=1,b=2):
Print A, b
>>> def haha (**kw):
Apply (say, (), kw)
>>> haha (a= ' a ', b= ' B ')
A b
MyThread (Super_play, (k,v), super_play.__name__)
Because the Mythread class inherits the Threading.thread class, we can use the Mythread class to create threads.
Operation Result:
The code is as follows:
Start Playing: Love business. mp3! Fri APR 25 10:36:19 2014
Start Playing: Avatar. mp4! Fri APR 25 10:36:19 2014
Start Playing: Love business. mp3! Fri APR 25 10:36:22 2014
Start Playing: Avatar. mp4! Fri APR 25 10:36:24 2014
All END:FRI APR 25 10:36:29 2014