1.Python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的、原始的线程以及一个简单的锁.Threading模块封装了一些常用的方法,初学者直接学这个模块就行了。
2.Python中使用线程有两种方式:函数或者用类来包装线程对象
3.threading.Thread里面几个参数介绍:
class Thread(_Verbose)
__init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None)
*group*:group参数必须为空,参数group是预留的,用于将来扩展;
参数args和kwargs分别表示调用target时的参数列表和关键字参数。
*target*: 参数target是一个可调用对象(也称为活动[activity]),在线程启动后执行
*name*: 参数name是线程的名字。默认值为“Thread-N“,N是一个数字。
*args*:传递给线程函数target的参数,他必须是个tuple类型.
*kwargs*:kwargs表示关键字参数。字典类型 {}.
1.先看个简单案例,这种是执行函数,函数不带参数的
# coding:utf-8
import threading
import time
def chi():
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())
time.sleep(1)
print("%s 吃火锅结束!" % time.ctime())
def ting():
print("%s 哼着小曲1!" % time.ctime())
time.sleep(2)
print("%s 哼着小曲2!" % time.ctime())
time.sleep(2)
print("%s 哼着小曲3!" % time.ctime())
time.sleep(2)
print("%s 哼着小曲4!" % time.ctime())
time.sleep(2)
print("%s 哼着小曲5!" % time.ctime())
time.sleep(2)
# 创建线程数组
threads = []
# 创建线程t1,并添加到线程数组
t1 = threading.Thread(target=chi)
threads.append(t1)
# 创建线程t2,并添加到线程数组
t2 = threading.Thread(target=ting)
threads.append(t2)
if __name__ == '__main__':
# 启动线程
for t in threads:
t.start()
2.带参数的用args传元组类型(参数最后多加一个逗号“,”要不然会报错)
# coding:utf-8
import threading
import time
def chi(threadName,name):
print("%s 吃着%s开始:" % (time.ctime(),threadName))
print("%s 吃着火锅:涮羊肉" % time.ctime())
time.sleep(1)
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 ting(threadName):
print("%s 哼着%s1!" % (time.ctime(),threadName))
time.sleep(2)
print("%s 哼着小曲2!" % time.ctime())
time.sleep(2)
print("%s 哼着小曲3!" % time.ctime())
time.sleep(2)
print("%s 哼着小曲4!" % time.ctime())
time.sleep(2)
print("%s 哼着小曲5!" % time.ctime())
time.sleep(2)
# 创建线程数组
threads = []
# 创建线程t1,并添加到线程数组
# t1 = threading.Thread(target=chi, args=("火锅","吃火锅",))
# 传kwargs参数
t1 = threading.Thread(target=chi, kwargs={"threadName":"火锅","name":"吃火锅"})
threads.append(t1)
# 创建线程t2,并添加到线程数组
t2 = threading.Thread(target=ting,args=("小曲",))
threads.append(t2)
if __name__ == '__main__':
# 启动线程
for t in threads:
t.start()
3.或者用kwargs传字典{}类型
# 创建线程t1,并添加到线程数组
# t1 = threading.Thread(target=chi, args=("火锅","吃火锅",))
# 传kwargs参数
t1 = threading.Thread(target=chi, kwargs={"threadName":"火锅","name":"吃火锅"})
标签:ctime,sleep,time,print,线程,多线程,target
From: https://www.cnblogs.com/djl-0628/p/17577258.html