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. Python multithreading is so simple. 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. The following is an example of continuous improvement: player. py copy Code # coding = utf-8from 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.mp3', 'add.mp4 '] threads = [] files = range (len (list) # create thread for I in files: t = threading. thread (target = player, args = (list [I],) threads. append (t) if _ name _ = '_ main _': # Start thread for I in files: threads [I]. start () for I in files: threads [I]. join () # Master thread print 'end: % s' % ctime () copy the code. Interestingly, we have created another player () function, this function is used 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 the code >>> x = 'testing. py '> s = x. split ('. ') [1] >>> if s = 'py': print s py copy the code running result: Start playing: lovebuy and sell! Mon Apr 21 12:48:40 2014 Start playing: mongoda .mp4! Mon Apr 21 12:48:40 2014 Start playing: the seller of love! Mon Apr 21 12:48:42 2014 Start playing: mongoda .mp4! Mon Apr 21 12:48:45 2014end: Mon Apr 21 12:48:50 2014 now add a file to the list array. A thread is automatically created when the program runs. Example of continuous improvement: Through the above program, we found that player () is used to determine the file extension, and then call music () and move (). In fact, music () and move () the complete work is the same. Why do we not use a super player? Any file can be played. After the transformation, my super player was born. Super_player.py copy Code # coding = utf-8from 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 of the files to be played and the playback duration = 'loveshop': 3,'add.mp4 ': 5, 'Your play ': 4} threads = [] files = range (len (list) # create a thread for file, time in list. items (): t = threading. thread (target = super_player, args = (file, time) threads. append (t) if _ name _ = '_ main _': # Start thread for I in files: threads [I]. start () for I in files: threads [I]. join () # Master thread print 'end: % s' % ctime () Copy code first create a dictionary list to define the timely length of the file to be played (Second), the dictionary items () method is used to cyclically retrieve the file and time. The obtained 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 the code Start playing: Love buyer! Fri Apr 25 09:45:09 2014 Start playing: Tell you success! Fri Apr 25 09:45:09 2014 Start playing: vod.mp4! Fri Apr 25 09:45:09 2014 Start playing: Love buyer! Fri Apr 25 09:45:12 2014 Start playing: Tell you success! Fri Apr 25 09:45:13 2014 Start playing: vod.mp4! Fri Apr 25 09:45:14 2014end: Fri Apr 25 09:45:19 2014 copy Code create your own multi-threaded copy Code # coding = utf-8import 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 shopaholics ': 3,'ida .mp4 ': 5} # create 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 thread for I in files: threads [I]. start () for I in files: threads [I]. join () # Master thread print 'end: % s' % ctime () copy the code MyThread (threading. create a MyThread class to inherit from threading. thread class. _ Init _ () uses the class initialization method to initialize parameters such as func, args, and name. The apply () 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 the Code # method without parameters> def say (): print 'say in' >>> apply (say) say in # The function only includes the parameter >>> def say (a, B): print a, B >>> apply (say, ('hello ', 'passer') hello passer' # function with keyword parameter >>> def say (a = 1, B = 2): print, B >>> def haha (** kw): apply (say, (), kw) >>> haha (a = 'A', B = 'B ') a B Copies the code MyThread (super_play, (k, v), super_play. _ name _) because the MyThread class inherits threading. thread class. Therefore, we can use the MyThread class to create a Thread. Running result: Start playing: Love buyer! Fri Apr 25 10:36:19 2014 Start playing: vod.mp4! Fri Apr 25 10:36:19 2014 Start playing: Love buyer! Fri Apr 25 10:36:22 2014 Start playing: vod.mp4! Fri Apr 25 10:36:24 2014all end: Fri Apr 25 10:36:29 2014