It took me a long time to understand python multithreading, and most of the articles I searched for were not easy to understand. So here we try to use a simple example to give you a preliminary understanding of multithreading.
Single thread
Many years ago in the MS-DOS era, the operating system to solve the problem is a single task, I want to listen to music and watch movies two things, then be sure to sort out the order first.
(Okay! In the DOS era, we don't need to worry about the applications of listening to music and watching movies. ^_^)
Copy codeThe 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 was at the movies! % S "% ctime ()
Sleep (5)
If _ name _ = '_ main __':
Music ()
Move ()
Print "all over % s" % ctime ()
We first listened to a piece of music and used the for loop to control the playing of the Music twice. It took one second to play each music and sleep () to control the playing duration. Then we watched another movie,
It takes five seconds for each movie. Because it looks so nice, I also read it twice through the for loop. After the whole leisure and entertainment activity
Copy codeThe Code is as follows:
Print "all over % s" % ctime ()
After reading the current time, it's almost time to go to bed.
Running result:
Copy codeThe 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 considered as music and video players. It is up to us to decide what songs and videos to play. Therefore, we modified the above Code:
Copy codeThe 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 was at the % s! % S "% (func, ctime ())
Sleep (5)
If _ name _ = '_ main __':
Music (u'love trading ')
Move (u'avatar ')
Print "all over % s" % ctime ()
Parameters of music () and move () are passed. Experience Chinese classic songs and major European and American cultures.
Running result:
Copy codeThe Code is as follows:
>>>================================== RESTART ================ ================================
>>>
I was listening to love trading. Thu Apr 17 11:48:59 2014
I was listening to love trading. 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
Technology is developing, the times are improving, and our CPU is getting faster and faster. CPU complaints. P takes some time for me. In fact, it's okay for me to do multiple jobs at the same time; therefore, the operating system
The system has entered the multi-task era. It's not our dream to listen to music and eat hot pot.
Python provides two modules to implement multi-thread and threading. thread has some shortcomings and can be compensated in threading. In order not to waste you and time, we directly
Then you can learn threading.
Continue to transform the above example and introduce threadring to play music and video simultaneously:
Copy codeThe 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 was at the % s! % S "% (func, ctime ())
Sleep (5)
Threads = []
T1 = threading. Thread (target = music, args = (u 'Sale of love ',))
Threads. append (t1)
T2 = threading. Thread (target = move, args = (u 'avant ',))
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 prerequisite for multithreading.
Copy codeThe Code is as follows:
Threads = []
T1 = threading. Thread (target = music, args = (u 'Sale of love ',))
Threads. append (t1)
The threads array is created, Thread t1 is created, and the threading. Thread () method is used. In this method, the music method target = music is called, and The args method is used to transmit parameters to music. Create
The created thread t1 is loaded into the threads array.
Then, create thread t2 in the same way and load the t2 to the threads array.
Copy codeThe Code is as follows:
For t in threads:
T. setDaemon (True)
T. start ()
Finally, the array is traversed through the for loop. (The array is loaded with t1 and t2 threads)
SetDaemon ()
SetDaemon (True) declares a thread as a daemon. It must be set before the start () method is called. If it is not set as a daemon, the program will be suspended infinitely. After the sub-thread starts, the parent thread
When the parent thread finishes executing the last statement print "all over % s" % ctime (), it does not wait for the sub-thread to exit directly, the sub-thread also ends.
Start ()
Start thread activity.
Running result:
Copy codeThe Code is as follows:
>>>============================== RESTART ============== ==================================
>>>
I was listening to love trading. Thu Apr 17 12:51:45 2014 I was at the Avatar! Thu Apr 17 12:51:45 2014 all over Thu Apr 17 12:51:45 2014
According to the execution results, both the Sub-thread (muisc, move) and the main thread (print "all over % s" % ctime () start at the same time, however, since the execution of the main thread ends, the sub-thread is terminated.
Continue the adjustment procedure:
Copy codeThe 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 add a join () method to the above program to wait for the thread to terminate. The function of join () is that the parent thread of this subthread will be blocked until the subthread completes running.
Note: The position of the join () method is outside the for loop. That is to say, the main process must be executed only after the two processes in the for loop are completed.
Running result:
Copy codeThe Code is as follows:
>>>============================== RESTART ============== ==================================
>>>
I was listening to love trading. Thu Apr 17 13:04:11 2014 I was at the Avatar! Thu Apr 17 13:04:11 2014
I was listening to love trading. 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
From the execution results, we can see that music and move are started at the same time.
The start time is 4 minutes 11 seconds until the main process called is 4 minutes 22 seconds and the total time is 10 seconds. It is reduced from a single thread by 2 seconds. We can adjust the sleep () Time of music to 4 seconds.
Copy codeThe 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:
Copy codeThe Code is as follows:
>>>=================================== RESTART ==================== ============================
>>>
I was listening to love trading. Thu Apr 17 13:11:27 2014I was at the Avatar! Thu Apr 17 13:11:27 2014
I was listening to love trading. 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 sub-thread starts for 11 minutes and 27 seconds, and the main thread runs for 11 minutes and 37 seconds.
Although each song of music has been extended from 1 second to 4, the total time for running the script through multiple threads remains unchanged.
This article allows you to quickly understand how to use multiple threads in python. For more details, see other documents or materials.
========================================================== ============================
Class threading. Thread () Description:
Copy codeThe Code is as follows:
Class threading. Thread (group = None, target = None, name = None, args = (), kwargs = {})
This constructor shoshould always be called with keyword arguments. Arguments are:
Group shoshould be None; reserved for future extension when a ThreadGroup class is implemented.
Target is the callable object to be invoked by the run () method. Defaults to None, meaning nothing is called.
Name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.
Args is the argument tuple for the target invocation. Defaults ().
Kwargs is a dictionary of keyword arguments for the target invocation. Defaults {}.
If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread. _ init _ () before doing
Anything else to the thread.
I have talked about a multi-thread blog before, and I feel that I am not doing anything about it. In fact, multithreading is very interesting. Because we are using multiple processes and threads all the time while using computers. We can continue with the previous example. Please read my previous blog first.
From the above example, it is quite troublesome to create a thread. Every time you create a thread, you need to create a tx (t1, t2 ,...), this is extremely inconvenient if many threads are created. Next we will continue to improve through examples:
Player. py
Copy codeThe 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 = require 'Love sale .mp3', 'ida .mp4 ']
Threads = []
Files = range (len (list ))
# Creating a thread
For I in files:
T = threading. Thread (target = player, args = (list [I],)
Threads. append (t)
If _ name _ = '_ main __':
# Start a thread
For I in files:
Threads [I]. start ()
For I in files:
Threads [I]. join ()
# Main thread
Print 'end: % s' % ctime ()
Interestingly, we have created another player () function to determine the type of the playback file. If it is in mp3 format, we will call the music () function. If it is in mp4 format, we will call the move () function. If neither of the two formats is used, you can only tell the user that the files you provide cannot be played.
Then, we create a list of objects. Note that the suffix is added to the object. Then we use len (list) to calculate the number of objects in the list. This is to help us determine the number of loops.
Next, we add the files in the list to the threads [] array in the thread through a for loop. Start the threads [] thread group and print the end time.
Split () can split a string into two parts and take one part.
Copy codeThe Code is as follows:
>>> X = 'testing. py'
>>> S = x. split ('.') [1]
>>> If s = 'py ':
Print s
Py
Running result:
Copy codeThe Code is as follows:
Start playing: Love buyer and seller! Mon Apr 21 12:48:40 2014
Start playing: .mp4! Mon Apr 21 12:48:40 2014
Start playing: Love buyer and seller! Mon Apr 21 12:48:42 2014
Start playing: .mp4! Mon Apr 21 12:48:45 2014
End: Mon Apr 21 12:48:50 2014
Add a file to the list array. A thread is automatically created when the program runs.
Example of further improvement:
Through the above program, we found that player () is used to determine the file extension and then call music () and move (). In fact, the complete work of music () and move () is the same, why don't we use a super player? No matter what file it is, it can be played. After the transformation, my super player was born.
Super_player.py
Copy codeThe 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)
# Playback files and playback duration
List = require 'Love shopaholics ': 3, 'ida .mp4': 5, 'Your sellers': 4}
Threads = []
Files = range (len (list ))
# Creating a thread
For file, time in list. items ():
T = threading. Thread (target = super_player, args = (file, time ))
Threads. append (t)
If _ name _ = '_ main __':
# Start a thread
For I in files:
Threads [I]. start ()
For I in files:
Threads [I]. join ()
# Main thread
Print 'end: % s' % ctime ()
First, create a dictionary list to define the duration (in seconds) of the file to be played. The dictionary items () method is used to obtain the file and time cyclically, these two values are used to create a thread.
Then create the super_player () function to receive the file and time, and determine the timely length of the file to be played.
Finally, the thread starts to run. Running result:
Copy codeThe Code is as follows:
Start playing: Love buyer and seller! Fri Apr 25 09:45:09 2014
Start playing: Tell you success! Fri Apr 25 09:45:09 2014
Start playing: .mp4! Fri Apr 25 09:45:09 2014
Start playing: Love buyer and seller! Fri Apr 25 09:45:12 2014
Start playing: Tell you success! Fri Apr 25 09:45:13 2014
Start playing: .mp4! Fri Apr 25 09:45:14 2014
End: Fri Apr 25 09:45:19 2014
Create your own multi-threaded class
Copy codeThe 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 = require 'Love shopaholics ': 3,'ida .mp4': 5}
# Creating a thread
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 __':
# Start a thread
For I in files:
Threads [I]. start ()
For I in files:
Threads [I]. join ()
# Main thread
Print 'end: % s' % ctime ()
MyThread (threading. Thread)
Create a MyThread class to inherit the threading. Thread class.
_ Init __()
Use the class initialization method to initialize parameters such as func, args, and name.
Apply ()
The apply (func [, args [, kwargs]) function is used to call a function indirectly when a function parameter already exists in a tuple or dictionary. Args is a tuple that contains parameters that will be provided to the function by location. If args is omitted, no parameters will be passed. kwargs is a dictionary containing key parameters.
Apply () usage:
Copy codeThe Code is as follows:
# Methods without Parameters
>>> Def say ():
Print 'say in'
>>> Apply (say)
Say in
# Functions only contain the parameters of tuples
>>> Def say (a, B ):
Print a, B
>>> Apply (say, ('hello', 'passer '))
Hello wormhole
# Function parameters with keywords
>>> 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 a Thread.
Running result:
Copy codeThe Code is as follows:
Start playing: Love buyer and seller! Fri Apr 25 10:36:19 2014
Start playing: .mp4! Fri Apr 25 10:36:19 2014
Start playing: Love buyer and seller! Fri Apr 25 10:36:22 2014
Start playing: .mp4! Fri Apr 25 10:36:24 2014
All end: Fri Apr 25 10:36:29 2014