可以通过实例化一个threading.Thread()对象来创建子线程. import threading import time def listen_music(num): print("----> %d" % num) print("begin to listen music at ", time.ctime()) time.sleep(5) print("end to listen music at ", time.ctime())…
1.方法一:将要执行的函数作为参数传递给threading.Thread() import threading import time def func(n): global count time.sleep(0.1) for i in range(n): count += 1 if __name__ == '__main__': count = 0 threads = [] for i in range(5): threads.append(threading.Thread(target=fu…
一.线程创建 #方法一:将要执行的方法作为参数传给Thread的构造方法 import threading import time def show(arg): time.sleep(2) print('thread' + str(arg)) for i in range(10): t = threading.Thread(target=show,args=(i,)) time.sleep(2) t.start() #方法2:从Thread继承,并重写run() class MyThread(t…