转载自上海-悠悠:https://www.cnblogs.com/yoyoketang/p/8269713.html
python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费你和时间,所以我们直接学习threading 就可以了。
Python中使用线程有两种方式:函数或者用类来包装线程对象
class Thread(_Verbose)
__init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None)
*group*:group参数必须为空,参数group是预留的,用于将来扩展;
*target*: 参数target是一个可调用对象(也称为活动[activity]),在线程启动后执行
*name*: 参数name是线程的名字。默认值为“Thread-N“,N是一个数字。
*args*:传递给线程函数target的参数,他必须是个tuple类型.
*kwargs*:kwargs表示关键字参数。字典类型 {}.
# 多进程函数式-无参数
import threading
import time
def eat():
print("%s吃着火锅开始"%time.ctime())
time.sleep(1)
print("%s吃着火锅:涮羊肉"%time.ctime())
time.sleep(1)
print("%s吃着火锅:涮牛肉" % time.ctime())
time.sleep(1)
print("%s吃火锅结束" % time.ctime())
def listen():
print("%s听音乐开始"%time.ctime())
time.sleep(1)
print("%s听音乐111"%time.ctime())
time.sleep(1)
print("%s听音乐222"%time.ctime())
time.sleep(1)
print("%s听音乐结束"%time.ctime())
if __name__ == ‘__main__‘:
threads=[]#创建线程组数
#创建线程1,并添加到线程组数
t1=threading.Thread(target=eat)
threads.append(t1)
#创建线程2,并添加到线程组数
t2=threading.Thread(target=listen)
threads.append(t2)
#启动线程
for i in threads:
i.start()

# 多进程函数式-带参数
import threading
import time
def eat(threadname,name):
print("%s吃着%s开始"%(time.ctime(),threadname))
time.sleep(1)
print("%s吃着火锅:涮羊肉"%time.ctime())
time.sleep(1)
print("%s吃着火锅:涮牛肉" % time.ctime())
time.sleep(1)
print("%s吃%s结束" % (time.ctime(),threadname))
print("%s运行结束"%name)
def listen(threadname):
print("%s听%s开始"%(time.ctime(),threadname))
time.sleep(1)
print("%s听音乐111"%time.ctime())
time.sleep(1)
print("%s听音乐222"%time.ctime())
time.sleep(1)
print("%s听音乐结束"%time.ctime())
if __name__ == ‘__main__‘:
threads=[]#创建线程组数
#参数使用args,传元组
t1=threading.Thread(target=eat,args=("火锅","吃火锅",))
#参数使用kwargs,传字典
# t1=threading.Thread(target=eat,kwargs={"threadname":"火锅","name":"吃火锅"})
threads.append(t1)
t2=threading.Thread(target=listen,args=("音乐",))
threads.append(t2)
#启动线程
for i in threads:
i.start()

注意:参数后面需要多加一个“,”,不然会报错。
我们可以通过直接从 threading.Thread 继承创建一个新的子类,并实例化后调用 start() 方法启动新线程,即它调用了线程的 run() 方法
#类封装式
import threading
import time
def eat(people):
print("%s吃火锅的小伙伴-羊肉:%s"%(time.ctime(),people))
time.sleep(1)
print("%s吃火锅的小伙伴-牛肉:%s"%(time.ctime(),people))
class myThread(threading.Thread):#继承父类的threading.Thread
def __init__(self,people,name):
‘‘‘重写threading.Thread初始化内容‘‘‘
threading.Thread.__init__(self)
self.threadname=name
self.people=people
def run(self):#把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
‘‘‘重写run方法‘‘‘
print("开始线程"+self.threadname)
eat(self.people)#执行任务
print("结束线程:%s"%self.threadname)
if __name__ == ‘__main__‘:
#创建线程
thread_1=myThread("小明","Thread-1")
thread_2=myThread("小红","Thread_2")
thread_1.start()
thread_2.start()
time.sleep(1)
print("退出主线程")

备注:这里运行结果会有个问题,主线程已经退出了,子线程Thread-1和Thread-2还在跑。这就是后面需要讲的守护线程了。。。
#类封装式
import threading
import time
def eat(people):
print("%s吃火锅的小伙伴-羊肉:%s"%(time.ctime(),people))
time.sleep(1)
print("%s吃火锅的小伙伴-牛肉:%s"%(time.ctime(),people))
class myThread(threading.Thread):#继承父类的threading.Thread
def __init__(self,people,name):
‘‘‘重写threading.Thread初始化内容‘‘‘
threading.Thread.__init__(self)
self.threadname=name
self.people=people
def run(self):#把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
‘‘‘重写run方法‘‘‘
print("开始线程"+self.threadname)
eat(self.people)#执行任务
print("结束线程:%s"%self.threadname)
if __name__ == ‘__main__‘:
#创建线程
thread_1=myThread("小明","Thread-1")
thread_2=myThread("小红","Thread_2")
thread_1.start()
thread_2.start()
time.sleep(1)
print("退出主线程")
原文:https://www.cnblogs.com/crystal1126/p/12808304.html