1. 引入第三方模块
1.1 import threading
import time
2. 实现timeit方法(函数运行时间)
2.1 def timeit(f):
def wrapper(*args, **kwargs):
start_time = time.time()
res = f(*args, **kwargs)
end_time = time.time()
print("%s函数运行时间为:%.2f" %(f.__name__, end_time - start_time))
return res
return wrapper
3. 模拟网上下载文件
3.1 def download_music(name):
time.sleep(2)
print("download music:%s......"%(name))
def download_movie(name):
time.sleep(2)
print("download movie:%s......"%(name))
4. 多线程实现下载
@timeit //监测多线程下载所用时间
def main():
t1 = threading.Thread(target=download_music, args=("哈哈哈",)) //args传递的是元组
t2 = threading.Thread(target=download_movie, args=("略略略",))
t1.start()
t2.start()
t1.join()
t2.join()
print("下载完成!")
main()