Create a thread class threading.py

import threading
from time import sleep,ctime
#创建线程类:
class MyThread(threading.Thread):
def __init__(self,func,args,name=""):
threading.Thread.__init__(self)
self.func = func
self.args = args
self.name = name

def run(self):
self.func(*self.args)

def super_play(file_,time):
for i in range(2):
print("Start playing: %s! %s" %(file_,ctime()))
sleep(time)

lists = {"爱情买卖.mp3":3,"阿凡达.mp4":5,"我和你.mp3":4}

threads = []
files = range(len(lists))

for file_,time in lists.items():
t = MyThread(super_play,(file_,time),super_play.__name__)
threads.append (t)

IF __name__ == '__main__':
# start the process:
for i in Files:
Threads [i] .start ()
for i in Files:
Threads [i] .join ()
Print ( "End:% S "% the ctime ())

" ""
Create MyThread class that inherits the class threading.Thread.
the __init__ class initializer for func, args, name and other parameters are initialized.
in the Python2, apply (func [, args [ , kwargs]]) function returns when the function parameters already exists in a tuple or dictionary, apply0 calling function .args indirectly, is a tuple of arguments passed by position to be provided to the function if omitted args, then any parameters will not be passed, kwargs is a dictionary containing the keyword arguments.
a Python 3 are no longer supported apply () function,
it will apply (self.func, self.args)
modified to
self.func ( * self.args)
Finally, the same as the previous example to create and start a thread, the only difference is to create threads that MyThread class, to the Senate in the form of thread has changed.
"" "

Guess you like

Origin www.cnblogs.com/zhang-da/p/12210542.html