'''
利用time函数,生产两个函数
顺序调用
计算总的运行时间
'''import time
defloopl():# ctime 得到当前时间
print('Statr loop l at : ', time.ctime())
# 睡眠多长时间,单位是秒
time.sleep(4)
print('End loop l at : ', time.ctime())
defloop2():# ctime 得到当前时间
print('Start loop 2 at : ', time.ctime())
# 睡眠多长时间,单位是秒
time.sleep(2)
print('End loop 2 at : ', time.ctime())
defmain():
print('Starting at : ', time.ctime())
loopl()
loop2()
print('All done at : ', time.ctime())
if __name__ == '__main__':
main()
Starting at : Sat Jun 30 14:35:17 2018
Statr loop l at : Sat Jun 30 14:35:17 2018
End loop l at : Sat Jun 30 14:35:21 2018
Start loop 2 at : Sat Jun 30 14:35:21 2018
End loop 2 at : Sat Jun 30 14:35:23 2018
All done at : Sat Jun 30 14:35:23 2018
启用多线程
# -*- coding:utf-8 -*-'''
利用time函数,生产两个函数
顺序调用
计算总的运行时间
'''import time
import _thread as thread
defloopl():# ctime 得到当前时间
print('Statr loop l at : ', time.ctime())
# 睡眠多长时间,单位是秒
time.sleep(4)
print('End loop l at : ', time.ctime())
defloop2():# ctime 得到当前时间
print('Start loop 2 at : ', time.ctime())
# 睡眠多长时间,单位是秒
time.sleep(2)
print('End loop 2 at : ', time.ctime())
defmain():
print('Starting at : ', time.ctime())
# 启动多线程的意思是用多线程去执行某个函数# 启动多线程函数为start_new_thread# 参数两个,一个是需要运行的函数名,第二个是函数的参数作为元祖使用,为空则使用空元祖# 注意⚠️:如果函数只有一个参数,需要参数后有一个逗号
thread.start_new_thread(loopl, ())
thread.start_new_thread(loop2, ())
print('All done at : ', time.ctime())
if __name__ == '__main__':
main()
time.sleep(5)
Starting at : Sat Jun 30 14:55:11 2018
All done at : Sat Jun 30 14:55:11 2018
Start loop 2 at : Sat Jun 30 14:55:11 2018
Statr loop l at : Sat Jun 30 14:55:11 2018
End loop 2 at : Sat Jun 30 14:55:13 2018
End loop l at : Sat Jun 30 14:55:15 2018
带参数的函数,多线程执行例子
# -*- coding:utf-8 -*-'''
利用time函数,生产两个函数
顺序调用
计算总的运行时间
'''import time
import _thread as thread
defloopl(param):# ctime 得到当前时间
print('Statr loop l at : ', time.ctime())
# 打印传入参数
print("参数 param is :" + param)
# 睡眠多长时间,单位是秒
time.sleep(4)
print('End loop l at : ', time.ctime())
defloop2(name, name2):# ctime 得到当前时间
print('Start loop 2 at : ', time.ctime())
# 打印参数
print("姓名1:" + name, "姓名2 :", name2)
# 睡眠多长时间,单位是秒
time.sleep(2)
print('End loop 2 at : ', time.ctime())
defmain():
print('Starting at : ', time.ctime())
# 启动多线程的意思是用多线程去执行某个函数# 启动多线程函数为start_new_thread# 参数两个,一个是需要运行的函数名,第二个是函数的参数作为元祖使用,为空则使用空元祖# 注意⚠️:如果函数只有一个参数,需要参数后有一个逗号
thread.start_new_thread(loopl, ("今天天气不错",))
thread.start_new_thread(loop2, ("王大拿","王呵呵"))
print('All done at : ', time.ctime())
if __name__ == '__main__':
main()
# 一定要有while语句,不然主线程结束,子线程也结束#while True:
time.sleep(5)
# -*- coding:utf-8 -*-'''
利用time函数,生产两个函数
顺序调用
计算总的运行时间
'''import time
import threading
defloopl(param):# ctime 得到当前时间
print('Statr loop l at : ', time.ctime())
# 打印传入参数
print("参数 param is :" + param)
# 睡眠多长时间,单位是秒
time.sleep(4)
print('End loop l at : ', time.ctime())
defloop2(name, name2):# ctime 得到当前时间
print('Start loop 2 at : ', time.ctime())
# 打印参数
print("姓名1:" + name, "姓名2 :", name2)
# 睡眠多长时间,单位是秒
time.sleep(2)
print('End loop 2 at : ', time.ctime())
defmain():
print('Starting at : ', time.ctime())
# 启动多线程的意思是用多线程去执行某个函数# 参数两个,一个是需要运行的函数名,第二个是函数的参数作为元祖使用,为空则使用空元祖# 注意⚠️:如果函数只有一个参数,需要参数后有一个逗号
t1 = threading.Thread(target=loopl, args=("老王头",))
t1.start()
t2 = threading.Thread(target=loop2, args=("张芃芃", "李冬面",))
t2.start()
print('All done at : ', time.ctime())
if __name__ == '__main__':
main()
Starting at : Sat Jun 30 15:11:00 2018
Statr loop l at : Sat Jun 30 15:11:00 2018
参数 param is :老王头
Start loop 2 at : All done at : Sat Jun 30 15:11:00 2018
Sat Jun 30 15:11:00 2018
姓名1:张芃芃 姓名2 : 李冬面
End loop 2 at : Sat Jun 30 15:11:02 2018
End loop l at : Sat Jun 30 15:11:04 2018
JOIN()用法
join等线程执行完成后继续往下执行
# -*- coding:utf-8 -*-'''
利用time函数,生产两个函数
顺序调用
计算总的运行时间
'''import time
import threading
defloopl(param):# ctime 得到当前时间
print('Statr loop l at : ', time.ctime())
# 打印传入参数
print("参数 param is :" + param)
# 睡眠多长时间,单位是秒
time.sleep(4)
print('End loop l at : ', time.ctime())
defloop2(name, name2):# ctime 得到当前时间
print('Start loop 2 at : ', time.ctime())
# 打印参数
print("姓名1:" + name, "姓名2 :", name2)
# 睡眠多长时间,单位是秒
time.sleep(2)
print('End loop 2 at : ', time.ctime())
defmain():
print('Starting at : ', time.ctime())
# 启动多线程的意思是用多线程去执行某个函数# 参数两个,一个是需要运行的函数名,第二个是函数的参数作为元祖使用,为空则使用空元祖# 注意⚠️:如果函数只有一个参数,需要参数后有一个逗号
t1 = threading.Thread(target=loopl, args=("老王头",))
t1.start()
t2 = threading.Thread(target=loop2, args=("张芃芃", "李冬面",))
t2.start()
# 等待两个线程都执行完毕,才继续往下执行
t1.join()
t2.join()
print('All done at : ', time.ctime())
if __name__ == '__main__':
main()
# -*- coding:utf-8 -*-'''
利用time函数,生产两个函数
顺序调用
计算总的运行时间
'''import time
import threading
defloopl(param):# ctime 得到当前时间
print('Statr loop l at : ', time.ctime())
# 打印传入参数
print("参数 param is :" + param)
# 睡眠多长时间,单位是秒
time.sleep(4)
print('End loop l at : ', time.ctime())
defloop2(name, name2):# ctime 得到当前时间
print('Start loop 2 at : ', time.ctime())
# 打印参数
print("姓名1:" + name, "姓名2 :", name2)
# 睡眠多长时间,单位是秒
time.sleep(2)
print('End loop 2 at : ', time.ctime())
defmain():
print('Starting at : ', time.ctime())
t1 = threading.Thread(target=loopl, args=("老王头",))
t1.setName("THR_1_loop1")
t1.start()
t2 = threading.Thread(target=loop2, args=("张芃芃", "李冬面",))
t2.setName("THR_2_loop2")
t2.start()
# 3秒后,线程2结束运行
time.sleep(3)
# enumerate 得到正在运行子线程,及子线程1for thr in threading.enumerate():
print("正在运行的线程名字是: {0}".format(thr.getName()))
print("正在运行的子线程数量为:{0}".format(threading.activeCount()))
print('All done at : ', time.ctime())
if __name__ == '__main__':
main()
Starting at : Sat Jun 30 15:39:41 2018
Statr loop l at : Sat Jun 30 15:39:41 2018
参数 param is :老王头
Start loop 2 at : Sat Jun 30 15:39:41 2018
姓名1:张芃芃 姓名2 : 李冬面
End loop 2 at : Sat Jun 30 15:39:43 2018
正在运行的线程名字是: MainThread
正在运行的线程名字是: Thread-2
正在运行的线程名字是: Thread-3
正在运行的线程名字是: IPythonHistorySavingThread
正在运行的线程名字是: Thread-1
正在运行的线程名字是: THR_1_loop1
正在运行的子线程数量为:6
All done at : Sat Jun 30 15:39:44 2018
End loop l at : Sat Jun 30 15:39:45 2018
直接继承自threading.Thread
- 直接继承Thread
- 重写run()
- 类实例可以直接运行
普通写法
# 直接继承threading.Thread 重写run方法import threading
import time
classThreadFunc(threading.Thread):def__init__(self):
threading.Thread.__init__(self)
defrun(self):global n, lock
time.sleep(1)
if lock.acquire():
print(n,self.name)
n, self.name
n += 1
lock.release()
if"__main__" == __name__:
n = 1
ThreadList = []
lock = threading.Lock()
for i in range(1, 200):
t = ThreadFunc()
ThreadList.append(t)
for t in ThreadList:
t.start()
for t in ThreadList:
t.join()
Starting at Sat Jun 30 16:00:49 2018
Start loop LOOP1 at Sat Jun 30 16:00:49 2018
Start loop LOOP2 at Sat Jun 30 16:00:49 2018
Done loop LOOP2 at Sat Jun 30 16:00:51 2018
Done loop LOOP1 at Sat Jun 30 16:00:53 2018
ALL done at Sat Jun 30 16:00:53 2018